Node.js 服务器中没有缓存 [英] No cache in Node.js server

查看:76
本文介绍了Node.js 服务器中没有缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过为了避免在 Node.js 中缓存,有必要使用:

I have read that to avoid caching in Node.js, it is necessary to use:

res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');

但我不知道如何使用它,因为当我将该行放入代码时会出错.

But I don't know how to use it because I get errors when I put that line in my code.

我的函数(我认为我必须对 Cache-Control 标头进行编程)是:

My function (where I think I have to program the Cache-Control header) is:

function getFile(localPath, mimeType, res) {
  fs.readFile(localPath, function(err, contents) {
    if (!err) {
      res.writeHead(200, {
        "Content-Type": mimeType,
        "Content-Length": contents.length,
        "Accept-Ranges": "bytes",
      });
      // res.header('Cache-Control', 'no-cache');
      res.end(contents);
    } else {
      res.writeHead(500);
      res.end();
    }
  });
}

有人知道如何在我的代码中不缓存吗?

Does anyone know how to put no cache in my code?

推荐答案

您已经编写了标题.我不认为您在完成之后可以添加更多内容,因此只需将标题放在您的第一个对象中即可.

You've already written your headers. I don't think you can add more after you've done that, so just put your headers in your first object.

res.writeHead(200, {
  'Content-Type': mimeType,
  'Content-Length': contents.length,
  'Accept-Ranges': 'bytes',
  'Cache-Control': 'no-cache'
});

这篇关于Node.js 服务器中没有缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆