node.js - 如何使用 nodejs 模块 http2 将 http2 与 ExpressJS 集成?

标签 node.js express 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] 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 enter image description here
For more details visit here
但是你可以使用 node-spdy .使用此模块,您可以在 node.js 中创建 HTTP2/SPDY 服务器,使用自然 http 模块接口(interface)并回退到常规 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' 支持 Node 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.

关于node.js - 如何使用 nodejs 模块 http2 将 http2 与 ExpressJS 集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59534717/

相关文章:

javascript - NPM ELIFECYCLE 错误 - 使用 node server.js 命令

http2 - 我可以在没有 ALPN 支持的情况下实现 http/2 服务器吗?

javascript - 用unicode字符提取字符串中的单词

javascript - 如何在 MEAN 堆栈中实现命中计数器?

javascript - Node js优化回调函数和nodemailer?

node.js - 托管 NodeJS 应用程序

python - 通过 HTTP2 提供 Python (Flask) REST API

webpack - 在 HTTP2 环境中从 Webpack2 包中提取 CSS 是否值得?

node.js - npm 错误!错误 : EPERM: operation not permitted, 重命名

javascript - 为什么 Node.js 事件循环需要多个阶段?