Nodejs Express 框架缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7790825/
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 14:41:25  来源:igfitidea点击:

Nodejs Express framework caching

node.jsexpress

提问by Tan Nguyen

I am using Nodejs and Express Js. Also I add NowJS to the Express Js to do some real-time stuffs.

我正在使用 Nodejs 和 Express Js。另外我将 NowJS 添加到 Express Js 来做一些实时的事情。

In the configuration file I have

在配置文件中我有

app.configure('production', function() {
var oneYear = 31557600000;
app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
app.use(express.errorHandler());});

And I run the application using this command:

我使用以下命令运行应用程序:

$ NODE_ENV=production node app.js

However, the files(images, css, js) seem not to be cached, they are always served as new file.

但是,文件(图像、css、js)似乎没有被缓存,它们总是作为新文件提供。

P/s: I have just tested it with localhost, the cache seems to work on localhost, however, when uploading to the server, the cache is not working anymore.

P/s:我刚刚用本地主机测试过,缓存似乎在本地主机上工作,但是,当上传到服务器时,缓存不再工作。

回答by alessioalex

Express is built on Connect, and Connect provides the "static" middleware. Here's the code under the hood for the caching:

Express 建立在 Connect 之上,Connect 提供“静态”中间件。这是缓存的底层代码:

if (!res.getHeader('Cache-Control')) res.setHeader('Cache-Control', 'public, max-age=' + (maxAge / 1000));

You can find that code here:

您可以在此处找到该代码:

https://github.com/senchalabs/connect/blob/master/lib/middleware/static.js#L147

https://github.com/senchalabs/connect/blob/master/lib/middleware/static.js#L147

So as you can see Express is sending a "Cache-Control" header to the browser, telling him to cache that file for a period. So this isn't a "load a file once and then always serve it to all clients", but more of a "tell each client to cache the file the first time he downloads it" (which means all the clients will have to download that file once before it's cached for them).

如您所见,Express 正在向浏览器发送“Cache-Control”标头,告诉浏览器将该文件缓存一段时间。所以这不是“加载一次文件,然后始终将其提供给所有客户端”,而是更多的“告诉每个客户端在第一次下载文件时缓存文件”(这意味着所有客户端都必须下载该文件在为他们缓存之前一次)。

回答by Felix

The following code does the job:

以下代码完成了这项工作:

app.use(function (req, res, next) {
    if (req.url.match(/^\/(css|js|img|font)\/.+/)) {
        res.setHeader('Cache-Control', 'public, max-age=3600'); // cache header
    }
    next();
});

回答by vashishatashu

The following code does the trick :

以下代码可以解决问题:

var cacheTime = 86400000*7;     // 7 days

app.use(express.static(__dirname + '/public',{ maxAge: cacheTime }));

However my public directory contains CSS as well as html files.

但是,我的公共目录包含 CSS 和 html 文件。

Is there a way i can cache css files only and not html.

有没有办法只缓存css文件而不缓存html。

I tried the setting "no-cache" in meta tag for html file but it didn't work.

我尝试在 html 文件的元标记中设置“无缓存”,但没有用。

回答by Bulat

EDIT: I was wrong, see eug's comment below

编辑:我错了,请参阅下面eug的评论

Connect includes caching middleware: http://senchalabs.github.com/connect/middleware-staticCache.html

Connect 包括缓存中间件:http: //senchalabs.github.com/connect/middleware-staticCache.html

so it should be as easy as

所以应该很简单

app.use(express.cache(...));
app.use(express.static(...));