当前是否可以获取 node .js HTTP/2(HTTP 2.0)服务器?以及http 2.0版本的express.js?

推荐答案

如果使用express@^5http2@^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.This is required if you want to accept TCP connections.

请注意,在 compose 本文时(2016 05 06),none of the major browsers support HTTP2 over TCP.

如果要接受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 */
    });

请注意,在 compose 本文时,我确实设法使expresshttp2正常工作(见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 */
    });

Node.js相关问答推荐

MongoServelSelection错误:服务器 Select 在30000 ms后超时

利用Gemini:通过Vertex AI还是通过Google/generative-ai?

(0,core_1.default)不是使用@middy/core的lambda处理程序上的函数

MongoDB如果文档S的字段小于另一个字段,则将该字段加一

FireStore事务似乎已允许并发编辑?

AWS-ROUTE 53指向S3存储桶,错误是别名目标名称不在目标区域内

如何使用NodeJS在mongodb中更新文档

使用pm2启动服务器

与诗乃一起嘲笑奈克斯

MongoDB - 查找查询以判断是否存在少量字段,结合字段上的 AND

使用 NPM 三个 mocha+typescript 进行测试

如何刷新 youtube-data-api v3 的访问令牌

类 WebSwapCGLLayer 在 Mac OS X /System 和 node_modules 中都实现了

2 x 匹配标准显示没有结果

什么是nestjs错误处理方式(业务逻辑错误vs.http错误)?

NodeJS为exec设置环境变量

node.js 在控制台上显示未定义

使用 Mongoose 进行多对多映射

使用 Monit 而不是基本的 Upstart 设置有什么好处?

react-native run-android 无法识别