NodeJS原生http2支持

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

有没有计划将http2支持合并到Node的核心中?

--expose-http2标志启用实验性--expose-http2支持。 自2017年8月5日起,此标志可用于夜间构build(节点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(); 

--experimental-modules节点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(节点版本切换器)来testing每晚构build。

 nvs add nightly nvs use nightly 

还没有。

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

节点8.4.0有一个实验性的Http2 API。 Docs here nodejs http2

从节点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公开创build一个服务器在这里 。