nodejs 服务器中没有缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20429592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:27:49  来源:igfitidea点击:

no cache in a nodejs server

node.jscaching

提问by user2986808

I have read that to avoid cache in nodejs it is necessary to use:

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

"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.

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

My function (where I think I have to program no cache) 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? thanks

有谁知道如何在我的代码中不缓存?谢谢

采纳答案by Brad

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

回答by vmx

Make use of a middleware to add no-cacheheaders. Use this middleware where-ever you intend to turn caching off.

使用中间件来添加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();
}

Use the middleware in your routes definition:

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

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

Let me know if this works for you.

让我知道这是否适合您。

回答by Pylinux

Set these headers on your response:

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

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

If you use express you can add this middleware to have no cache on all requests:

如果你使用 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()
});

回答by Lanil Marasinghe

You can use nocacheMiddleware to turn off caching.

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

npm install --save nocache

Apply the middleware to your app

将中间件应用于您的应用

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

This disables browser caching.

这将禁用浏览器缓存。

回答by bmw15

Pylinux's answer worked for me, but upon further inspection, I found the helmet module for express that handles some other security features for you.

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

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

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

To use, install and require helmet in express.js, then call app.use(helmet.noCache());

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

回答by Dale Anderson

After spelunking through source code for the express and fresh modules, this works from the server side (before res.end is called):

在深入了解 express 和 fresh 模块的源代码后,这在服务器端起作用(在调用 res.end 之前):

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

Nasty, but it works.

讨厌,但它有效。