目前是否可以获得node.js HTTP / 2(HTTP 2.0)服务器?和http 2.0版本的express.js?
答案 0 :(得分:26)
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);
修改强>
答案 1 :(得分:13)
如果您使用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
。 This 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 */
});
请注意,在撰写本文时,我确实设法让express
和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 */
});
答案 2 :(得分:1)
自 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。
答案 3 :(得分:0)
这个问题至今仍然存在(一年之后),所以我决定制定一个解决方法,使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);
});
上面的代码是一个工作快速应用程序,它使用nodejs http模块(用于HTTP / 1.x)和http2模块(用于HTTP / 2)。
如自述文件中所述,这将创建新的express请求和响应对象,并将其原型设置为http2的IncomingMessage和ServerResponse对象。默认情况下,它是内置nodejs http IncomingMessage和ServerResponse对象。
我希望这会有所帮助:)