阿西河

所有教程

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

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js HTTP2 服务器端示例

      Node.js HTTP2 服务器端示例

      The following illustrates a simple HTTP/2 server using the Core API. Since there are no browsers known that support unencrypted HTTP/2, the use of http2.createSecureServer() is necessary when communicating with browser clients.

      const http2 = require('http2');
      const fs = require('fs');
      
      const server = http2.createSecureServer({
        key: fs.readFileSync('localhost-privkey.pem'),
        cert: fs.readFileSync('localhost-cert.pem')
      });
      server.on('error', (err) => console.error(err));
      
      server.on('stream', (stream, headers) => {
        // stream is a Duplex
        stream.respond({
          'content-type': 'text/html',
          ':status': 200
        });
        stream.end('<h1>Hello World</h1>');
      });
      
      server.listen(8443);
      

      To generate the certificate and key for this example, run:

      openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
        -keyout localhost-privkey.pem -out localhost-cert.pem
      

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


      目录
      本文目录
      目录