设置了Cache-Controll: 'max-age=10000',但是不起作用。Disable cache的复选框我已经取消了。
来源:3-4 缓存头Cache-Control的含义和使用

guaguaerhao
2019-10-11
// server.js
const http = require('http')
const fs = require('fs')
const path = require('path')
http.createServer((req, res)=>{
console.log('request come', req.url)
if (req.url === '/') {
const html = fs.readFileSync(path.resolve(__dirname, 'test.html'),'utf8')
res.writeHead(200,{
"Content-Type": "text/html"
})
res.end(html)
}
if (req.url === '/script.js') {
res.writeHead(200,{
'Content-Type': 'text/javascript',
'Cache-Control': 'max-age=1001232'
})
res.end('console.log("script loaded")')
}
}).listen(8888)
console.log('server listening 8888')
// server2.js
const http = require('http')
http.createServer((req, res)=>{
res.writeHead(200,{
"Access-Control-Allow-Origin": "*",
'Access-Control-Allow-Headers': 'X-Test-Cors',
"Access-Control-Allow-Methods": "PUT,GET,POST",
"Access-Control-Max-Age": '10000'
})
res.end('123')
}).listen(8887)
console.log('server listening 8887')
<!--test.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- <script>
var xhr = new XMLHttpRequest()
xhr.open('GET', 'http://127.0.0.1:8887/')
xhr.send()
</script> -->
<!-- jsonp -->
<!-- <script src="http://127.0.0.1:8887?callback=jsonp"></script> -->
<script>
fetch('http://127.0.0.1:8887',{
method: 'PUT',
header:{
"X-Test-Cors": "123"
}
})
</script>
</body>
</html>
老师,我按照课程设置了Cache-Control: ‘max-age=123123’,缓存时间,但是浏览器每一次刷新都是获取新的script.js,而不是从缓存中获取。帮我看一下哪里出问题了。
写回答
1回答
-
guaguaerhao
提问者
2019-10-11
解决了,发现自己犯了个错误。html中script的src是/script.js,
还有server.js文件的Content-Type:'application/javascript'。
问题就不删除了,留个印象。
012019-10-13
相似问题