node.js 服务器和 HTTP/2 (2.0) 与 express.js

node.js server and HTTP/2 (2.0) with express.js

目前是否可以获取 node.js HTTP/2 (HTTP 2.0) 服务器?和 http 2.0 版本 express.js?

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);

编辑

这段代码摘自a conversation on Github.

如果您使用的是express@^5http2@^3.3.4,那么启动服务器的正确方法是:

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 */
    });

注意 https2.rawThis is required if you want to accept TCP connections.

请注意,在撰写本文时 (2016 05 06),none of the major browsers support HTTP2 over TCP

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

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 */
    });

请注意,在撰写本文时,我确实设法使 expresshttp2 正常工作(请参阅 https://github.com/molnarg/node-http2/issues/100#issuecomment-217417055). However, I have managed to get http2 (and SPDY) to work using 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 */
    });

这个问题今天仍然存在(写这篇文章时是 2016 年),所以我决定尝试做一个解决方法,让 express 和 http2 包很好地协同工作:https://www.npmjs.com/package/express-http2-workaround

编辑: 由于本机 'http2' 模块,不适用于 v8.4 以上的任何 NodeJS 版本。

通过 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 对象。

希望对您有所帮助:)

自 2018 年以来有一个 express 5.0 的公开 pr,https://github.com/expressjs/express/pull/3730。在合并之前,它不会开箱即用。

我已经以包的形式创建了解决方案,https://www.npmjs.com/package/http2-express-bridge

const express = require('express')
const http2Express = require('http2-express-bridge')
const http2 = require('http2')
const { readFileSync } = require('fs')

// Use the wrapper function that returns the application
const app = http2Express(express)

const options = {
    key: readFileSync('<Certificate Key>'),
    cert: readFileSync('<Certificate file>'),
    allowHTTP1: true
};


app.get('/', function (req, res) {
  res.send('Hello World')
})

const server = http2.createSecureServer(options, app)

server.listen(3000, () => {
        console.log(`listening on port 3000`)
})

这有效,当它收到 Http/1.1 请求时,它会回退到 Http/1.1。

我还包含了 'res.push' 方法以简化服务器推送。该软件包适用于 ESModules 和 Typescript。