当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
属性。
我想通了。问题不在于ExpressJs
或npm scripts
,而在于我使用的 shell 命令。
使用console.dir
时,我看到NODE_ENV
设置为production
带有额外空间。将 npm 脚本更改为 "start": "set NODE_ENV=production&&node ./bin/www"
确实有效。