阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js http2 ALPN 协商

      Node.js http2 ALPN 协商

      ALPN negotiation allows supporting both HTTPS and HTTP/2 over the same socket. The req and res objects can be either HTTP/1 or HTTP/2, and an application must restrict itself to the public API of HTTP/1, and detect if it is possible to use the more advanced features of HTTP/2.

      The following example creates a server that supports both protocols:

      
      const { createSecureServer } = require('http2');
      const { readFileSync } = require('fs');
      
      const cert = readFileSync('./cert.pem');
      const key = readFileSync('./key.pem');
      
      const server = createSecureServer(
        { cert, key, allowHTTP1: true },
        onRequest
      ).listen(4443);
      
      function onRequest(req, res) {
        // detects if it is a HTTPS request or HTTP/2
        const { socket: { alpnProtocol } } = req.httpVersion === '2.0' ?
          req.stream.session : req;
        res.writeHead(200, { 'content-type': 'application/json' });
        res.end(JSON.stringify({
          alpnProtocol,
          httpVersion: req.httpVersion
        }));
      }
      
      

      The ‘request’ event works identically on both HTTPS and HTTP/2.


      更多内容请参考:Node.js HTTP2,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录