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

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

如果您使用express@^5http2@^3.3.4 express@^5 http2@^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.raw 。 如果要接受TCP连接,则这是必需的。

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

如果要接受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 )。 不过,我设法使用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 */ }); 
 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上的一个对话中获得的。

这个问题还在今天(一年以后),所以我决定做一个解决scheme,使express和http2包很好地结合在一起。 我创build了一个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); }); 

上面的代码是使用nodejs http模块(用于HTTP / 1.x)和http2模块(用于HTTP / 2)的工作快速应用程序。

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

我希望这有帮助 :)