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

下面是http1.1的例子,通過對比發(fā)現(xiàn)瀏覽器按照自己的最大并發(fā)量同時發(fā)出請求,只有當請求返回后才發(fā)出新的請求(瀏覽器開啟限流和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);
服務端推送
按照如下代碼測試
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,

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

綜上所看,服務端推送可以提前加載資源,優(yōu)化非首頁加載有益。
令人高興的是,因為使用率低,chrome在105版本后不再支持http2的服務端推送,導致這個特點在前端開發(fā)中可以忽略了。并且如果要測試改特點需要使用低版本的chrome,比如本例子使用的是chrome 96 mac版本。
本文所用代碼:https://github.com/blank-x/pg/tree/master/http2,nodejs版本是v16.19.0.
總結(jié)
到此這篇關于如何在nodejs中體驗http/2的文章就介紹到這了,更多相關nodejs中體驗http/2內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
什么時候不能在 Node.js 中使用 Lock Files
這篇文章主要介紹了什么時候不能在 Node.js 中使用 Lock Files,但是當你在開發(fā)要發(fā)布到 npm 的包時,應避免使用這類 lock file 。下面我們來一起了解一下吧2019-06-06
使用nodejs實現(xiàn)JSON文件自動轉(zhuǎn)Excel的工具(推薦)
這篇文章主要介紹了使用nodejs實現(xiàn),JSON文件自動轉(zhuǎn)Excel的工具,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
解決Node.js使用MySQL出現(xiàn)connect ECONNREFUSED 127.0.0.1:3306的問題
這篇文章主要介紹了解決Node.js使用MySQL出現(xiàn)connect ECONNREFUSED 127.0.0.1:3306報錯的相關資料,文中將問題描述的很清楚,解決的方法也介紹的很完整,需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03

