Home  >  Q&A  >  body text

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信息:

伊谢尔伦伊谢尔伦2766 days ago542

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 14:51:58

    1. Force it

    2. Have you turned on disable cache in the console?

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 14:51:58

    It has been solved, it was a refresh problem. Manual refresh will force the browser to go to the server. As long as you reopen the current page in a new window, you can see 200 (from cache)

    reply
    0
  • Cancelreply