// node.js 示例代碼
const http = require('http');
const fs = require('fs');
const url = require('url');
const md5 = require('md5');
const port = process.argv[2]; // 獲取端口號(hào)
if (!port) {
console.log('請(qǐng)指定端口號(hào)~~~');
process.exit(1);
}
const server = http.createServer((request, response) => {
const path = request.url;
const parsedUrl = url.parse(path, true);
const query = path.indexOf('?') >= 0 ? path.substring(path.indexOf('?')) : '';
const pathNoQuery = parsedUrl.pathname; // 不包含查詢
const queryObject = parsedUrl.query; // 查詢參數(shù)
const method = request.method; // 請(qǐng)求方法
switch (pathNoQuery) {
case '/':
let htmlStr = fs.readFileSync('./index.html', 'utf8');
response.setHeader('Content-type', 'text/html; charset=utf-8');
response.write(htmlStr);
break;
case '/index.html':
let htmlStr2 = fs.readFileSync('./index.html', 'utf8');
response.setHeader('Content-type', 'text/html; charset=utf-8');
response.write(htmlStr2);
break;
case '/default.css':
let cssStr = fs.readFileSync('./css/default.css', 'utf8');
response.setHeader('Content-type', 'text/css; charset=utf-8');
response.write(cssStr);
break;
case '/main.js':
let jsStr = fs.readFileSync('./js/main.js', 'utf8');
response.setHeader('Content-type', 'text/javascript; charset=utf-8');
response.write(jsStr);
break;
default:
response.statusCode = 404;
break;
}
response.end();
});
server.listen(port);
console.log('監(jiān)聽 ' + port + ' 成功\n請(qǐng)打開 http://localhost:' + port);
cache-control
設(shè)置靜態(tài)文件過(guò)期時(shí)間
86400*365 = 31536000 一年的秒數(shù)
// 表示30s以內(nèi)不要再請(qǐng)求我霹陡,用緩存數(shù)據(jù)印衔。
response.setHeader('Cache-control', 'max-age=31536000');
一般在response header中設(shè)置靴跛,比如在下面的main.js添加
case '/main.js':
let jsStr = fs.readFileSync('./js/main.js', 'utf8');
response.setHeader('Content-type', 'text/javascript; charset=utf-8');
response.setHeader('Cache-control', 'max-age=31536000'); // 注意這一行
response.write(jsStr);
break;
cache-control
main.js大小共1.1M,
第一次請(qǐng)求的時(shí)間為8.60s;
第二次請(qǐng)求時(shí)間為0s, 讀取from memory cache,注意此時(shí)狀態(tài)碼是 200,不是304巨柒。
由于沒有給default.css加cache-control,所以任然會(huì)向服務(wù)器發(fā)起請(qǐng)求,耗時(shí)600多毫秒柠衍。
Expires
// 跟cache-control的區(qū)別是cache-control定義的是失效時(shí)長(zhǎng)洋满,expires定義的是失效時(shí)間點(diǎn)
response.setHeader('Expires', 'Thu, 06 Sep 2018 12:52:52 GMT');
eTag
md5是一種文件摘要算法。etag值一般為文件對(duì)應(yīng)的md5值珍坊。
一般客戶端請(qǐng)求一個(gè)文件后牺勾,設(shè)置response header
const md5 = require('md5'); // 頭部引入
let cssStr = fs.readFileSync('./css/default.css', 'utf8');
// response.setHeader('Cache-control', 'max-age=86400');
const fileMd5 = md5(cssStr);
response.setHeader('eTag', fileMd5); // 響應(yīng)頭設(shè)置ETag屬性
如下圖所示,第一次請(qǐng)求default.css時(shí)阵漏,響應(yīng)頭中有ETag屬性驻民。
第一次請(qǐng)求時(shí)
比對(duì)文件MD5的值,一樣則返回304
let cssStr = fs.readFileSync('./css/default.css', 'utf8');
// response.setHeader('Cache-control', 'max-age=86400');
const fileMd5 = md5(cssStr);
response.setHeader('ETag', fileMd5);
const ifNoneMatch = request.headers['if-none-match'];
if (ifNoneMatch === fileMd5) {
response.statusCode = 304;
} else {
response.setHeader('Content-type', 'text/css; charset=utf-8');
response.write(cssStr);
}
重點(diǎn)履怯!回还,下次刷新后,在request header中會(huì)出現(xiàn) If-None-Match字段叹洲。
image.png
后端在header獲得If-No-Match的值與fileMd5值相比較柠硕,如果不一樣說(shuō)明文件更新了,則后端繼續(xù)返回最新的文件运提。如果一樣蝗柔,則說(shuō)明不需要更新闻葵,返回code: 304和一個(gè)空的response。
cache-control: 直接不請(qǐng)求癣丧,用本地緩存笙隙。
eTag: 請(qǐng)求,比較md5值坎缭,一樣則返回空的相應(yīng)體,不一樣則返回最新的文件签钩。
Last-Modified&If-Modify-Since
last-modified
訪問(wèn)服務(wù)器時(shí)Request Header會(huì)自動(dòng)帶上If-Modified-Since字段掏呼,服務(wù)器會(huì)拿這個(gè)時(shí)間跟服務(wù)器文件最后更新時(shí)間去比,判斷是否需要更新铅檩。