当前是否可以获取node.js HTTP / 2(HTTP 2.0)服务器?以及Express.js的HTTP 2.0版本?
如果使用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日),所有主流浏览器都不支持基于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 */
});
请注意,在写这篇文章的时候,我还是设法使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 */
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句