如何在nodejs中體驗(yàn)http/2詳解
前言
2015年,HTTP/2 發(fā)布,直到2021年公司的項(xiàng)目才開(kāi)始在實(shí)踐中應(yīng)用;自己對(duì)http2諸多特點(diǎn)的理解只存在于字面上,于是嘗試在nodejs中實(shí)踐一下,加深自己的理解。
多路復(fù)用
同域名下所有通信都在單個(gè)連接上完成,消除了因多個(gè) TCP 連接而帶來(lái)的延時(shí)和內(nèi)存消耗,這在大量請(qǐng)求同時(shí)發(fā)出的情況下能夠減少加載時(shí)間。
使用如下代碼查看http2環(huán)境下,資源下載的情況(瀏覽器開(kāi)啟限流和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);
可以看到當(dāng)資源開(kāi)始同時(shí)請(qǐng)求,所有的請(qǐng)求形成一個(gè)隊(duì)列,請(qǐng)求之間開(kāi)始時(shí)間相差大概1ms, 因?yàn)橄螺d的是同一個(gè)圖片,50張圖片同時(shí)下載,最后幾乎在同時(shí)完成下載。

下面是http1.1的例子,通過(guò)對(duì)比發(fā)現(xiàn)瀏覽器按照自己的最大并發(fā)量同時(shí)發(fā)出請(qǐng)求,只有當(dāng)請(qǐng)求返回后才發(fā)出新的請(qǐng)求(瀏覽器開(kāi)啟限流和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);
服務(wù)端推送
按照如下代碼測(cè)試
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, 同時(shí)首頁(yè)加載的大小是207 B,

注釋掉stream.pushStream部分后,不使用推送,資源加載如下,style.css大小是89B, 同時(shí)首頁(yè)加載的大小是182B,

綜上所看,服務(wù)端推送可以提前加載資源,優(yōu)化非首頁(yè)加載有益。
令人高興的是,因?yàn)槭褂寐实?,chrome在105版本后不再支持http2的服務(wù)端推送,導(dǎo)致這個(gè)特點(diǎn)在前端開(kāi)發(fā)中可以忽略了。并且如果要測(cè)試改特點(diǎn)需要使用低版本的chrome,比如本例子使用的是chrome 96 mac版本。
本文所用代碼:https://github.com/blank-x/pg/tree/master/http2,nodejs版本是v16.19.0.
總結(jié)
到此這篇關(guān)于如何在nodejs中體驗(yàn)http/2的文章就介紹到這了,更多相關(guān)nodejs中體驗(yàn)http/2內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
node簡(jiǎn)單實(shí)現(xiàn)一個(gè)更改頭像功能的示例
本篇文章主要介紹了node簡(jiǎn)單實(shí)現(xiàn)一個(gè)更改頭像功能的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
什么時(shí)候不能在 Node.js 中使用 Lock Files
這篇文章主要介紹了什么時(shí)候不能在 Node.js 中使用 Lock Files,但是當(dāng)你在開(kāi)發(fā)要發(fā)布到 npm 的包時(shí),應(yīng)避免使用這類 lock file 。下面我們來(lái)一起了解一下吧2019-06-06
NodeJs操作MongoDB教程之分頁(yè)功能以及常見(jiàn)問(wèn)題
這篇文章主要給大家介紹了關(guān)于NodeJs操作MongoDB教程之分頁(yè)功能以及常見(jiàn)問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用NodeJs具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
使用nodejs實(shí)現(xiàn)JSON文件自動(dòng)轉(zhuǎn)Excel的工具(推薦)
這篇文章主要介紹了使用nodejs實(shí)現(xiàn),JSON文件自動(dòng)轉(zhuǎn)Excel的工具,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
解決Node.js使用MySQL出現(xiàn)connect ECONNREFUSED 127.0.0.1:3306的問(wèn)題
這篇文章主要介紹了解決Node.js使用MySQL出現(xiàn)connect ECONNREFUSED 127.0.0.1:3306報(bào)錯(cuò)的相關(guān)資料,文中將問(wèn)題描述的很清楚,解決的方法也介紹的很完整,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-03-03
ChatGPT編程秀之最小元素的設(shè)計(jì)示例詳解
這篇文章主要為大家介紹了ChatGPT編程秀之最小元素的設(shè)計(jì)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

