HTTP--http之缓存验证Last-Modifed和Etag(六)

文档

clipboard.png

Last-Modified
  1. 上次修改时间
  2. 配合If-Modified-Since或者If-Unmodified-Since使用
Etag
  1. 数据签名
  2. 配合If-Match或者If-Non-Match使用
  3. 对比资源的签名判断是否使用缓存

文件内容

server3.js
const http = require('http');
const fs = require('fs');
http.createServer(function(req, res) {
    console.log('request come', req.url);
    const html = fs.readFileSync('index.html', 'utf8');
    if (req.url === '/') {
        res.writeHead(200, {
            "Content-Type":"text/html"
        })
        res.end(html)   
    }
    if (req.url === '/script.js') {
        const etag = req.headers['if-none-match'];
        if (etag === '777') {
            res.writeHead(304, {
                "Content-Type": "text/javascript",
                "Cache-Control": "max-age=200000,no-cache",
                'Last-Modified': "123",
                "Etag":"777"
            })
            res.end(' ')   
        } else {
            res.writeHead(200, {
                "Content-Type": "text/javascript",
                "Cache-Control": "max-age=200000,no-cache",
                'Last-Modified': "123",
                "Etag":"777"
            })
            res.end('console.log("script loaded 222")')      
        }
    }
}).listen(8888)
console.log('server start at port 8888')

clipboard.png

clipboard.png

修改server3.js

const http = require('http');
const fs = require('fs');
http.createServer(function(req, res) {
    console.log('request come', req.url);
    const html = fs.readFileSync('index.html', 'utf8');
    if (req.url === '/') {
        res.writeHead(200, {
            "Content-Type":"text/html"
        })
        res.end(html)   
    }
    if (req.url === '/script.js') {
        const etag = req.headers['if-none-match'];
        if (etag === '777') {
            res.writeHead(304, {
                "Content-Type": "text/javascript",
                "Cache-Control": "max-age=200000",
                'Last-Modified': "123",
                "Etag":"777"
            })
            res.end('123')   
        } else {
            res.writeHead(200, {
                "Content-Type": "text/javascript",
                "Cache-Control": "max-age=200000",
                'Last-Modified': "123",
                "Etag":"777"
            })
            res.end('console.log("script loaded 222")')      
        }
    }
}).listen(8888)
console.log('server start at port 8888')

clipboard.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值