node.js服务器和带有express.js的HTTP / 2(2.0)

当前是否可以获取node.js HTTP / 2(HTTP 2.0)服务器? 以及Express.js的HTTP 2.0版本?

WHITECOLOR asked 2020-08-07T07:20:28Z
3个解决方案
26 votes
var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('hello, http2!');
});

var options = {
  key: fs.readFileSync('./example/localhost.key'),
  cert: fs.readFileSync('./example/localhost.crt')
};

require('http2').createServer(options, app).listen(8080);

编辑

该代码段摘自Github上的一次对话。

HDK answered 2020-08-07T07:21:17Z
16 votes

如果使用的是express@^5spdy,则启动服务器的正确方法是:

const http2 = require('http2');
const express = require('express');

const app = express();

// app.use('/', ..);

http2
    .raw
    .createServer(app)
    .listen(8000, (err) => {
        if (err) {
            throw new Error(err);
        }

        /* eslint-disable no-console */
        console.log('Listening on port: ' + argv.port + '.');
        /* eslint-enable no-console */
    });

请注意spdy。如果要接受TCP连接,这是必需的。

请注意,在撰写本文时(2016年5月6日),所有主流浏览器都不支持基于TCP的HTTP2。

如果要接受TCP和TLS连接,则需要使用默认的spdy方法启动服务器:

const http2 = require('http2');
const express = require('express');
const fs = require('fs');


const app = express();

// app.use('/', ..);

http2
    .createServer({
        key: fs.readFileSync('./localhost.key'),
        cert: fs.readFileSync('./localhost.crt')
    }, app)
    .listen(8000, (err) => {
        if (err) {
            throw new Error(err);
        }

        /* eslint-disable no-console */
        console.log('Listening on port: ' + argv.port + '.');
        /* eslint-enable no-console */
    });

请注意,在撰写本文时,我确实设法使3000556923977977024和http2正常工作(请参阅[https://github.com/molnarg/node-http2/issues/100#issuecomment-217417055)。]但是,我有 使用spdy软件包成功使http2(和SPDY)正常工作。

const spdy = require('spdy');
const express = require('express');
const path = require('path');
const fs = require('fs'); 

const app = express();

app.get('/', (req, res) => {
    res.json({foo: 'test'});
});

spdy
    .createServer({
        key: fs.readFileSync(path.resolve(__dirname, './localhost.key')),
        cert: fs.readFileSync(path.resolve(__dirname, './localhost.crt'))
    }, app)
    .listen(8000, (err) => {
        if (err) {
            throw new Error(err);
        }

        /* eslint-disable no-console */
        console.log('Listening on port: ' + argv.port + '.');
        /* eslint-enable no-console */
    });
Gajus answered 2020-08-07T07:20:52Z
1 votes

这个问题在今天(一年后)仍然存在,所以我决定尝试一种解决方法,以使Express和http2软件包很好地协同工作。我创建了一个npm软件包,该软件包确实可以做到这一点:[https://www.npmjs.com/package/express-http2-workaround]

通过NPM安装:npm install express-http2-workaround --save

// Require Modules
var fs = require('fs');
var express = require('express');
var http = require('http');
var http2 = require('http2');

// Create Express Application
var app = express();

// Make HTTP2 work with Express (this must be before any other middleware)
require('express-http2-workaround')({ express:express, http2:http2, app:app });

// Setup HTTP/1.x Server
var httpServer = http.Server(app);
httpServer.listen(80,function(){
  console.log("Express HTTP/1 server started");
});

// Setup HTTP/2 Server
var httpsOptions = {
    'key' : fs.readFileSync(__dirname + '/keys/ssl.key'),
    'cert' : fs.readFileSync(__dirname + '/keys/ssl.crt'),
    'ca' : fs.readFileSync(__dirname + '/keys/ssl.crt')
};
var http2Server = http2.createServer(httpsOptions,app);
http2Server.listen(443,function(){
  console.log("Express HTTP/2 server started");
});

// Serve some content
app.get('/', function(req,res){
    res.send('Hello World! Via HTTP '+req.httpVersion);
});

上面的代码是一个有效的Express应用程序,它同时使用nodejs http模块(对于HTTP / 1.x)和http2模块(对于HTTP / 2)。

如自述文件所述,这将创建新的快速请求和响应对象,并将其原型设置为http2的IncomingMessage和ServerResponse对象。 默认情况下,它是内置的nodejs http IncomingMessage和ServerResponse对象。

我希望这有帮助 :)

Jashepp answered 2020-08-07T07:21:55Z
translate from https://stackoverflow.com:/questions/28639995/node-js-server-and-http-2-2-0-with-express-js