【问题标题】:NodeJS native http2 supportNodeJS 原生 http2 支持
【发布时间】:2016-05-28 23:56:02
【问题描述】:

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

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

【问题讨论】:

    标签: node.js http2


    【解决方案1】:

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

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

    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
    

    【讨论】:

    【解决方案2】:

    不,还没有。

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

    【讨论】:

      【解决方案3】:

      从节点 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);
      

      我已经写了更多关于使用native HTTP/2 Node.js exposes to create a server here 的内容。

      【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 你知道nodejs中的http2支持什么时候不再是实验性的吗?
      猜你喜欢
      • 2021-06-14
      • 2019-10-02
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      相关资源
      最近更新 更多