首页 > 编程语言 >在nodejs中体验http/2

在nodejs中体验http/2

时间:2022-12-20 23:24:14浏览次数:56  
标签:fs const stream nodejs http2 server 体验 http path

前言

2015年,HTTP/2 发布,直到2021年公司的项目才开始在实践中应用;自己对http2诸多特点的理解只存在于字面上,于是尝试在nodejs中实践一下,加深自己的理解。

多路复用

同域名下所有通信都在单个连接上完成,消除了因多个 TCP 连接而带来的延时和内存消耗,这在大量请求同时发出的情况下能够减少加载时间。

使用如下代码查看http2环境下,资源下载的情况(浏览器开启限流和disable cache):

const http2 = require('http2');
const fs = require('fs');
const { HTTP2_HEADER_PATH } = http2.constants;

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
  const path = headers[':path'];
  if(path === '/img.png' || path === '/favicon.ico'){
    const fd = fs.openSync('img.png', 'r');
    const stat = fs.fstatSync(fd);
    const headers = {
      'content-length': stat.size,
      'last-modified': stat.mtime.toUTCString(),
      'content-type': 'image/png'
    };
    stream.respondWithFD(fd, headers);

  } else if(path === '/') {
    stream.respond({
      'content-type': 'text/html; charset=utf-8',
      ':status': 200
    });
    stream.end(`
      <h1>Hello World</h1>
      <script>
        for(var i=0;i<50;i++){
          fetch('/img.png')
        }
      </script>
   
    `);
  }
});

server.listen(8443);

可以看到当资源开始同时请求,所有的请求形成一个队列,请求之间开始时间相差大概1ms, 因为下载的是同一个图片,50张图片同时下载,最后几乎在同时完成下载。
image

下面是http1.1的例子,通过对比发现浏览器按照自己的最大并发量同时发出请求,只有当请求返回后才发出新的请求(浏览器开启限流和disable cache):


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

const server = http.createServer(function(req,res){
  const path = req.url;
  if(path === '/img.png' || path === '/favicon.ico'){
    res.writeHead(200,{'Content-type':'image/png'})
    var stream = fs.createReadStream('img.png')
    stream.pipe(res)
  } else {
    res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
    res.end(`
      <h1>Hello World</h1>
      <script>
        for(var i=0;i<50;i++){
          fetch('/img.png')
        }
      </script>
    `);
  }
});


server.listen(8444);

image

服务端推送

按照如下代码测试

const http2 = require('http2');
const fs = require('fs');
const { HTTP2_HEADER_PATH } = http2.constants;

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) => {
  const path = headers[':path'];
  if(path === '/') {
    stream.respond({
      'content-type': 'text/html; charset=utf-8',
      ':status': 200
    });

    stream.pushStream({ [HTTP2_HEADER_PATH]: '/style.css' }, (err, pushStream, headers) => {
      if (err) throw err;
      const fd = fs.openSync('style.css', 'r');
      const stat = fs.fstatSync(fd);
      const header = {
        'content-length': stat.size,
        'last-modified': stat.mtime.toUTCString(),
        'content-type': 'text/css'
      };
      pushStream.respondWithFD(fd, header)
    });

    stream.end(`
      <h1>Hello World</h1>
      <script>
        setTimeout(()=>{
          fetch('/style.css')
        },2000)
      </script>
    `);
  } else if(path === '/style.css'){

    const fd = fs.openSync('style.css', 'r');
    const stat = fs.fstatSync(fd);
    const headers = {
      'content-length': stat.size,
      'last-modified': stat.mtime.toUTCString(),
      'content-type': 'text/css'
    };
    stream.respondWithFD(fd, headers);
  }

});

server.listen(8442);

资源加载情况如下,style.css的Initiator是Push,大小是66 B, 同时首页加载的大小是207 B,
image
注释掉stream.pushStream部分后,不使用推送,资源加载如下,style.css大小是89B, 同时首页加载的大小是182B,

image
综上所看,服务端推送可以提前加载资源,优化非首页加载有益。

令人高兴的是,因为使用率低,chrome在105版本后不再支持http2的服务端推送,导致这个特点在前端开发中可以忽略了。并且如果要测试改特点需要使用低版本的chrome,比如本例子使用的是chrome 96 mac版本。

本文所用代码:https://github.com/blank-x/pg/tree/master/http2,nodejs版本是v16.19.0.

标签:fs,const,stream,nodejs,http2,server,体验,http,path
From: https://www.cnblogs.com/walkermag/p/16995319.html

相关文章

  • HttpClient Timeout waiting for connection from pool 问题解决方案
    错误:org.apache.http.conn.ConnectionPoolTimeoutException:Timeoutwaitingforconnectionfrompool前言:第一次看到这个错误,上网找了下,有文章说是连接池不够了。。。......
  • Flask - Flask中的HTTP方法和URL组成部分
    一、前言参考:http://www.imooc.com/wiki/flasklesson/flaskhttp.html上篇我们创建了第一个Flask程序,并且在浏览器中成功看到了Flask程序给我们返回的HelloWorld。......
  • ajax的statusText在http1.1和http2中的不同
    问题在服务端支持协议从http1.1升级到http2的时候,项目中正常的请求流程出现了错误,经排查是ajax封装代码使用xmlHttpRequest的statusText属性判断是否正常返回,进而导致错误......
  • HTTP & HTTPS
    1.HTTPSvsHTTPhttps://www.cnblogs.com/enhance/p/15766413.html2.HTTPS三次握手第一步:客户端向服务器发送HTTPS请求,服务器将公钥以证书的形式发送到客户端(服......
  • HttpURLConnection 传输文件
    packageext.integration.k2;importorg.apache.commons.codec.binary.Base64;importjava.io.*;importjava.net.HttpURLConnection;importjava.net.URL;importjava.n......
  • 使用HTTP代理之后仍显示本地 IP ,有推荐靠谱的HTTP代理厂商吗?
    是不是高匿,主要还是看HTTP_X_FORWARDED_FOR的参数是什么样的,我们也可以从这里来判断自己使用的HTTP代理是不是高匿。高匿代理:HTTP_X_FORWARDED_FOR=RandomIP address普匿代......
  • puppeteer( Nodejs 版 selenium )快速入门
    puppeteer官网:​​https://pptr.dev/​​Puppeteer中文文档(与官Puppeteer中文文档 :​​https://learnku.com/docs/puppeteer/3.1.0​​Puppeteerv1.5.0中文翻peteer......
  • Python 异步 IO 、协程、asyncio、async/await、aiohttp
    From:廖雪峰 异步IO:​​https://www.liaoxuefeng.com/wiki/1016959663602400/1017959540289152​​PythonAsync/Await入门指南:​​https://zhuanlan.zhihu.com/p/27258......
  • 如何进行“花式”HTTP接口测试
    文章目录​​最Old-School的方式​​​​最普通的方式​​​​最文艺的方式​​​​最认真的方式​​​​最“期望”的方式​​​​最“偷懒”的方式​​​​最“理想”的方......
  • 软件测试培训 | 在霍格沃兹测试开发学社学习是种怎样的体验?
    霍格沃兹我怎么了解到的我是河北某二本院校软工专业的学生,大三开始学校来了很多宣讲和实训的公司,都是为我们以后的职业发展做参考。学校有软件测试课程,有一次老师无意提到......