当前是否可以获取 node .js HTTP/2(HTTP 2.0)服务器?以及http 2.0版本的express.js?
当前是否可以获取 node .js HTTP/2(HTTP 2.0)服务器?以及http 2.0版本的express.js?
如果使用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.
请注意,在 compose 本文时(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 */
});
请注意,在 compose 本文时,我确实设法使express
和http2
正常工作(见https://github.com/molnarg/node-http2/issues/100#issuecomment-217417055).然而,我已经设法让http2(和SPDY)使用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 */
});