如何使用 nodejs 模块 http2 将 http2 与 ExpressJS 集成?
How to integrate http2 with ExpressJS using nodejs module http2?
我正在用 nodejs 和 express 创建一个 api,我想将 http2 与 ExpressJS 集成
这是我的代码:
'use strict';
const http2 = require('http2');
const fs = require('fs');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 443;
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Routes variables
const indexRouter = require('./routes/index');
// Routes uses
app.use('/', indexRouter);
// Server configurations
const key = path.join(__dirname + '/security/key.pem');
const cert = path.join(__dirname + '/security/certificate.pem');
const options = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert)
}
const server = http2.createSecureServer(options, app);
server.on('error', err => console.log(err));
server.listen(port, () => {
console.log('Server running')
})
我正在尝试将 express server 作为 createSecureServer() 的第二个参数传递,但我不确定我这样做是否正确,因为我收到此错误:
[nodemon] 2.0.2 [nodemon] to restart at any time, enter rs
[nodemon]
watching dir(s): . [nodemon] watching extensions: js,mjs,json
[nodemon] starting node index.js
_http_incoming.js:96 if (this.socket.readable)
^
TypeError: Cannot read property 'readable' of undefined
at IncomingMessage._read (_http_incoming.js:96:19)
at IncomingMessage.Readable.read (stream_readable.js:491:10)
at resume (_stream_readable.js:976:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) [nodemon] app crashed -
waiting for file changes before starting...
请注意,我的证书虽然是自签名的且不可靠,但加载正确。
如果可以使用 NodeJS,我尽量不使用第三方模块。有帮助吗?
expressjs
仍未正式支持 Node http2
但是你可以使用node-spdy
。使用此模块,您可以在 node.js 中创建具有自然 http 模块接口的 HTTP2 / SPDY 服务器,并回退到常规 https(对于既不支持 HTTP2 也不支持 SPDY 的浏览器)。
const port = 3000
const spdy = require('spdy')
const express = require('express')
const path = require('path')
const fs = require('fs')
const app = express()
app.get('*', (req, res) => {
res
.status(200)
.json({message: 'ok'})
})
const options = {
key: fs.readFileSync(__dirname + '/server.key'),
cert: fs.readFileSync(__dirname + '/server.crt')
}
console.log(options)
spdy
.createServer(options, app)
.listen(port, (error) => {
if (error) {
console.error(error)
return process.exit(1)
} else {
console.log('Listening on port: ' + port + '.')
}
})
有关 spdy
、visit here 的更多信息。
如果你有其他框架的选项,你可以使用 'KOA' 或 'HAPI' 有支持对于节点 http2
。 This might be useful for you
此外,请阅读此 Release 5.0#2237,它说:
The goal of Express 5 is to be API tweaks & the removal of all code
from the Express repository, moving into components in the pillarjs
project (https://github.com/pillarjs), providing at least basic
support for promise-returning handlers and complete HTTP/2
functionality. Express 5 would become a "view into pillarjs" and would
be an arrangement of these components.
我正在用 nodejs 和 express 创建一个 api,我想将 http2 与 ExpressJS 集成
这是我的代码:
'use strict';
const http2 = require('http2');
const fs = require('fs');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 443;
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Routes variables
const indexRouter = require('./routes/index');
// Routes uses
app.use('/', indexRouter);
// Server configurations
const key = path.join(__dirname + '/security/key.pem');
const cert = path.join(__dirname + '/security/certificate.pem');
const options = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert)
}
const server = http2.createSecureServer(options, app);
server.on('error', err => console.log(err));
server.listen(port, () => {
console.log('Server running')
})
我正在尝试将 express server 作为 createSecureServer() 的第二个参数传递,但我不确定我这样做是否正确,因为我收到此错误:
[nodemon] 2.0.2 [nodemon] to restart at any time, enter
rs
[nodemon] watching dir(s): . [nodemon] watching extensions: js,mjs,json [nodemon] startingnode index.js
_http_incoming.js:96 if (this.socket.readable) ^TypeError: Cannot read property 'readable' of undefined at IncomingMessage._read (_http_incoming.js:96:19) at IncomingMessage.Readable.read (stream_readable.js:491:10) at resume (_stream_readable.js:976:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) [nodemon] app crashed - waiting for file changes before starting...
请注意,我的证书虽然是自签名的且不可靠,但加载正确。 如果可以使用 NodeJS,我尽量不使用第三方模块。有帮助吗?
expressjs
仍未正式支持 Node http2
但是你可以使用node-spdy
。使用此模块,您可以在 node.js 中创建具有自然 http 模块接口的 HTTP2 / SPDY 服务器,并回退到常规 https(对于既不支持 HTTP2 也不支持 SPDY 的浏览器)。
const port = 3000
const spdy = require('spdy')
const express = require('express')
const path = require('path')
const fs = require('fs')
const app = express()
app.get('*', (req, res) => {
res
.status(200)
.json({message: 'ok'})
})
const options = {
key: fs.readFileSync(__dirname + '/server.key'),
cert: fs.readFileSync(__dirname + '/server.crt')
}
console.log(options)
spdy
.createServer(options, app)
.listen(port, (error) => {
if (error) {
console.error(error)
return process.exit(1)
} else {
console.log('Listening on port: ' + port + '.')
}
})
有关 spdy
、visit here 的更多信息。
如果你有其他框架的选项,你可以使用 'KOA' 或 'HAPI' 有支持对于节点 http2
。 This might be useful for you
此外,请阅读此 Release 5.0#2237,它说:
The goal of Express 5 is to be API tweaks & the removal of all code from the Express repository, moving into components in the pillarjs project (https://github.com/pillarjs), providing at least basic support for promise-returning handlers and complete HTTP/2 functionality. Express 5 would become a "view into pillarjs" and would be an arrangement of these components.