问题描述
目前是否可以获取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。端口+'。');
/ * eslint-enable no-console * /
});
请注意
https2.raw
。
如果要接受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
正常工作(请参阅)。但是,我设法使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 * /
});
Is it possible currently to get node.js HTTP/2 (HTTP 2.0) server? And http 2.0 version of express.js?
解决方案
If you are using
express@^5
and http2@^3.3.4
, then the correct way to start the server is:
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 */
});
Notice the
https2.raw
. This is required if you want to accept TCP connections.
Note that at the time of this writing (2016 05 06), none of the major browsers support HTTP2 over TCP.
If you want to accept TCP and TLS connections, then you need to start the server using the default
createServer
method:
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 */
});
Note that at the time of this writing, I did manage to make
express
and http2
to work (see https://github.com/molnarg/node-http2/issues/100#issuecomment-217417055). However, I have managed to get http2 (and SPDY) to work using spdy
package.
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 */
});
这篇关于node.js服务器和HTTP / 2(2.0)与express.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!