node.js 将 Node/Express 应用程序设置为实时或生产模式的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29984319/
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
Best way to set a Node/Express app to live or production mode
提问by Owen C. Jones
I'm currently in the process of making my node/express app into a production deployment, and as part of this, I need to make it run in a production friendly mode (e.g. fewer debugs to stdOut, write logs to different places, tell users less when errors occur etc.).
我目前正在将我的 node/express 应用程序制作成生产部署,作为其中的一部分,我需要让它在生产友好模式下运行(例如,减少对 stdOut 的调试,将日志写入不同的地方,告诉发生错误时用户减少等)。
I'm struggling a bit with this, as whenever I set a variable of virtually any kind to invoke a 'production' mode, it doesn't take affect for the program as it runs.
我对此有点挣扎,因为每当我设置几乎任何类型的变量来调用“生产”模式时,它都不会在程序运行时产生影响。
When launched in dev mode, my code runs through Gulp, and runs this script:
在开发模式下启动时,我的代码通过 Gulp 运行,并运行以下脚本:
#!/usr/bin/env node
var debug = require('debug')('ascema');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
Which, as you know, is just the generated launch script from the express generator.
如您所知,这只是快速生成器生成的启动脚本。
In order to run it in live mode, I created an alternative startup for the server to use (I could hardly use gulp anyway) and live.js runs this:
为了在实时模式下运行它,我创建了一个可供服务器使用的替代启动(无论如何我几乎无法使用 gulp)并且 live.js 运行这个:
#!/usr/bin/env node
var app = require('./app.js');
app.set('env', 'production');
console.log('running on Port 3000');
var server = app.listen(3000);
But, when I use app.get('env')anywhere in the app (e.g. in app.js or in it's various dependencies) it still returns 'development' and so none of my production tasks happen.
但是,当我app.get('env')在应用程序的任何地方(例如在 app.js 或它的各种依赖项中)使用时,它仍然返回“开发”,因此我的生产任务都没有发生。
What am I doing wrong here?
我在这里做错了什么?
Many thanks.
非常感谢。
回答by Noman Ur Rehman
You have to set the NODE_ENVvariable on the command line when you execute your NodeJS application.
NODE_ENV当您执行 NodeJS 应用程序时,您必须在命令行上设置该变量。
For example:
例如:
NODE_ENV=production node app.js
Also, NODE_ENVis an environment variable so if set it in the environment on your server, you will not need to provide it every time you execute your application so node app.jswill do.
此外,NODE_ENV是一个环境变量,因此如果在服务器上的环境中设置它,则每次执行应用程序时都不需要提供它,这样node app.js就可以了。
You can set your environment variables in the `/etc/environment' file. Here are more details on that: https://help.ubuntu.com/community/EnvironmentVariables
你可以在`/etc/environment' 文件中设置你的环境变量。以下是更多详细信息:https: //help.ubuntu.com/community/EnvironmentVariables
回答by Pax Beach
By using NPM you might to be used the follow scripts in the package.json:
通过使用 NPM,您可能会在 package.json 中使用以下脚本:
"scripts": {
"start": "nodemon ./bin/www",
"dev_win": "set NODE_ENV=development && node ./bin/www >> /romba/log/api.log 2>> /romba/log/error.log",
"prod_win": "set NODE_ENV=production && node ./bin/www >> /romba/log/api.log 2>> /romba/log/error.log"
"prod_nix": "NODE_ENV=production node ./bin/www >> /romba/log/api.log 2>> /romba/log/_error.log"
},...
To start one of the script use the command:
要启动脚本之一,请使用以下命令:
npm run-script prod_win
In the JavaScript code I check the condition:
在 JavaScript 代码中,我检查了条件:
process.env.NODE_ENV.indexOf('production') > -1
or with Express.js
或使用 Express.js
app.use((err, req, res, next) => {
req.app.get('env') === 'development' ? ...

