【问题标题】:No cache in Node.js serverNode.js 服务器中没有缓存
【发布时间】:2013-12-24 03:22:07
【问题描述】:

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

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

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

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

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();
    }
  });
}

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

【问题讨论】:

  • 如果您想混合使用res.header()(和/或res.setHeader()),请在res.writeHead() 之前使用。 writeHead() 是最终的,因此不能在其后添加额外的标题。
  • 嗨,谢谢.. 我之前尝试过放置 res.header() 但出现错误。另一方面 res.setHeader 似乎工作

标签: node.js caching


【解决方案1】:

您已经编写了标题。我不认为你可以在完成之后添加更多,所以只需将你的标题放在你的第一个对象中。

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

【讨论】:

  • 如果你使用res.set,你可以在编写你的标题之后设置标题,在res.write触发之前,可​​能自'13以来已经改变
【解决方案2】:

利用中间件添加no-cache 标头。在您打算关闭缓存的任何地方使用此中间件。

function nocache(req, res, next) {
  res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
  res.header('Expires', '-1');
  res.header('Pragma', 'no-cache');
  next();
}

在路由定义中使用中间件:

app.get('/getfile', nocache, sendContent);

function sendContent(req, res) {
  var localPath = 'some-file';
  var mimeType = '';
  fs.readFile(localPath, 'utf8', function (err, contents) {
    if (!err && contents) {
      res.header('Content-Type', mimeType);
      res.header('Content-Length', contents.length);
      res.end(contents);
    } else {
      res.writeHead(500);
      res.end();
    }
  });
}

让我知道这是否适合你。

【讨论】:

  • 他没有使用 Express。
  • 嗨 vmx...谢谢,但布拉德是对的我没有使用 Express,我认为您的回答仅使用 Express 很有用...
  • 好吧,即使你不使用 express,本质上需要的是设置 nocache 头。我在可重用中间件中添加标头,否则您可以以任何可行的方式设置这些标头。
  • 我不认为使用或不使用快递很重要。
【解决方案3】:

在您的响应中设置这些标题:

'Cache-Control': 'private, no-cache, no-store, must-revalidate'
'Expires': '-1'
'Pragma': 'no-cache'

如果您使用 express,您可以添加此中间件以对所有请求不缓存:

// var app = express()
app.use(function (req, res, next) {
    res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
    res.header('Expires', '-1');
    res.header('Pragma', 'no-cache');
    next()
});

【讨论】:

  • 这与@vmx 的答案有何不同/改进?
【解决方案4】:

在对 express 和 fresh 模块的源代码进行深入研究之后,这可以从服务器端工作(在调用 res.end 之前):

req.headers['if-modified-since'] = undefined;
req.headers['if-none-match'] = undefined;

讨厌,但它有效。

【讨论】:

    【解决方案5】:

    Pylinux 的回答对我有用,但经过进一步检查,我发现 express 的头盔模块可以为您处理一些其他安全功能。

    http://webapplog.com/express-js-security-tips/

    要在 express.js 中使用、安装和 require 头盔,然后调用app.use(helmet.noCache());

    【讨论】:

      【解决方案6】:

      您可以使用 nocache 中间件来关闭缓存。

      npm install --save nocache
      

      将中间件应用到您的应用中

      const nocache = require('nocache');
      ...
      app.use(nocache());
      

      这会禁用浏览器缓存。

      【讨论】:

        猜你喜欢
        • 2014-11-03
        • 2015-07-01
        • 1970-01-01
        • 2020-07-03
        • 1970-01-01
        • 1970-01-01
        • 2022-09-25
        • 2013-02-12
        • 1970-01-01
        相关资源
        最近更新 更多