javascript - 如何在 Nodejs 中缓存 .js 和 .css 以及其他静态文件?

标签 javascript node.js express caching pug

我尝试了很多,但我无法缓存我使用 JADE 模板引擎构建在 Nodejs Express 上的 Web 应用程序。

我试过的是,

1- 试过 pm2 管理器 2- 尝试将 NODE_ENV 设置为 production 3- 添加了缓存模块(但我不知道如何使用它)

const NodeCache = require( "node-cache" );
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 120 } );

但它没有缓存。

这是 https://nodejs.org/en/ 的结果 enter image description here

这是我的网络应用程序的结果, enter image description here

我需要缓存所有的静态文件如何做到这一点..请帮助我..谢谢

最佳答案

handler 方法中设置 Cache-Control header 。

app.get('/', function(req, res){
    res.set('Cache-Control', 'max-age=31536000');
    res.send('hello world');
});

客户端缓存不需要使用node-cache

关于javascript - 如何在 Nodejs 中缓存 .js 和 .css 以及其他静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842771/

相关文章:

javascript - 没有旋转的转换属性的 CSS 转换

javascript - jQuery 点击动态添加的类

node.js - MongoDB Node findone如何处理没有结果?

node.js - Express 应用程序在 azure 上扔了 500 个

javascript - jQuery Galleria.js 可以加载非图像文件类型,例如 .html 文件或其他内容类型,例如 HTML5 视频吗?

javascript - 如果出现 404,为什么网页会永远加载?

javascript - Node.js 在控制台中打印回未定义的属性

javascript - Express 无法获取/图像

javascript - 如何使用 Express 的静态中间件正确加载图像?

javascript - 使用 `Error: Could not locate the bindings file.` 时如何解析 `node-ffi` ?