ExpressJs 在生产模式下不缓存视图



NODE_ENV设置为production时,我在ExpressJs不缓存我的views时遇到了问题。我正在使用EJS template engine.这是我的代码:

npm 脚本

"start": "set NODE_ENV=production && node ./bin/www"

应用.js

console.log(app.get('env')) => logs production
console.log(app.get('view cache')) => logs undefined
app.set('view cache', true)
console.log(app.get('view cache')) => logs true

因此,当我将NODE_ENV环境变量设置为 production ExpressJs 时,似乎不会启用视图缓存,但是当我通过调用app.set('view cache', true)手动启用它时,它会。根据ExpressJs它应该默认启用视图缓存。

我确实测试了缓存是否有效,但只有在我手动启用它时才有效。

首先通过NODE_ENV=production node app.js运行您的应用程序,如果它true,您应该在 package.json 中更改您的 start 属性。

我想通了。问题不在于ExpressJsnpm scripts,而在于我使用的 shell 命令。

使用console.dir时,我看到NODE_ENV设置为production带有额外空间。将 npm 脚本更改为 "start": "set NODE_ENV=production&&node ./bin/www"确实有效。

最新更新