繁体   English   中英

如何使用 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')
})

我试图将快速服务器作为 createSecureServer() 的第二个参数传递,但我不确定我是否正确,因为我收到此错误:

[nodemon] 2.0.2 [nodemon] 随时重启,输入rs [nodemon] watching dir(s): [nodemon] 观看扩展:js,mjs,json [nodemon] 起始node index.js _http_incoming.js:96 if (this.socket.readable) ^

TypeError:无法在 IncomingMessage._read (_http_incoming.js:96:19) 在 IncomingMessage.Readable.read ( stream_readable.js:491:10) 在 resume (_stream_readable.js:976:12) 处读取未定义的属性“可读” processTicksAndRejections (internal/process/task_queues.js:80:21) [nodemon] 应用程序崩溃 - 在开始之前等待文件更改...

应该注意的是,我的证书虽然是自签名的且不可靠,但可以正确加载。 如果可以使用 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更多信息,请访问此处

如果您可以选择其他框架,则可以使用支持节点http2 “KOA”“HAPI” 这可能对你有用

另外,请阅读此版本 5.0#2237 ,它说:

Express 5 的目标是调整 API 并从 Express 存储库中删除所有代码,移至 pillarjs 项目( https://github.com/pillarjs )中的组件,至少为承诺返回处理程序提供基本支持和完整的 HTTP/2 功能。 Express 5 将成为“对 pillarjs 的看法”,并且将是这些组件的排列。

express 不知道底层协议,所以你可以这样做:

const app = require('express')()
require('http2').createServer(options, app).listen(8080);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM