html5 - node静态资源服务器设置了Cache-Control,但浏览器从来不走304
本文介绍了html5 - node静态资源服务器设置了Cache-Control,但浏览器从来不走304的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
我使用node作静态资源服务器,返回一张普通的png图片。
现在我给这个响应设置了Cecha-Control,希望可以让浏览器进行缓存。可是每次我刷新页面,都是返回200,从服务器请求资源。请问应该如何设置才能使用浏览器的缓存呢?
下面是node端代码:
const fs = require('fs')
const http = require('http')
const url = require('url')
const server = http.createServer((req, res) => {
let pathname = url.parse(req.url).pathname
let realPath = 'assets' + pathname
console.log(realPath)
fs.readFile(realPath, "binary", function(err, file) {
if (err) {
res.writeHead(500, {'Content-Type': 'text/plain'})
res.end(err)
} else {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'image/png',
'ETag': "666666",
'Cache-Control': 'max-age=31536000, public',
'Expires': 'Mon, 07 Sep 2026 09:32:27 GMT'
})
res.write(file, "binary")
res.end()
}
})
})
server.listen(80)
console.log('Listening on port: 80')
请求header信息:
解决方案
强刷了吧
控制台开启了 disable cache 了吧
这篇关于html5 - node静态资源服务器设置了Cache-Control,但浏览器从来不走304的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文