繁体   English   中英

NodeJS本机http2支持

[英]NodeJS native http2 support

NodeJS 4.x或5.x本机是否支持HTTP / 2协议? 我知道有http2包,但它是一个外部的东西。

是否有计划将http2支持合并到Node的核心?

--expose-http2 flag支持实验性--expose-http2 自2017年8月5日起,此标志可用于夜间构建(Node v8.4.0)( 拉取请求 )。

node --expose-http2 client.js

client.js

const http2 = require('http2');
const client = http2.connect('https://stackoverflow.com');

const req = client.request();
req.setEncoding('utf8');

req.on('response', (headers, flags) => {
  console.log(headers);
});

let data = '';
req.on('data', (d) => data += d);
req.on('end', () => client.destroy());
req.end();

也可以从Node v8.5.0开始添加--experimental-modules标志。

node --expose-http2 --experimental-modules client.mjs

client.mjs

import http2 from 'http2';

const client = http2.connect('https://stackoverflow.com');

我使用NVS(节点版本切换器)来测试夜间构建。

nvs add nightly
nvs use nightly

还没有。

以下是关于向核心NodeJS添加HTTP / 2支持的讨论: https//github.com/nodejs/NG/issues/8

从节点v8.8.1开始,运行代码时不需要--expose-http2标志。

开始使用HTTP / 2的最简单方法是使用Node.js公开的兼容性API。

const http2 = require('http2');
const fs = require('fs');

const options = {
    key: fs.readFileSync('./selfsigned.key'),
    cert: fs.readFileSync('./selfsigned.crt'),
    allowHTTP1: true
}

const server = http2.createSecureServer(options, (req, res) => {
  res.setHeader('Content-Type', 'text/html');
  res.end('ok');
});

server.listen(443);

我已经写了更多关于使用本机HTTP / 2 Node.js公开来创建服务器的信息

Node 8.4.0有一个实验性的Http2 API。 这里的文件是nodejs http2

暂无
暂无

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

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