Node.js、express 以及在 app.configure 中使用开发与生产
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10714315/
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
Node.js, express, and using development versus production in app.configure
提问by Tampa
What is the easiest method to let express know what environment I am in? E.g. I want do do the below to make a connection to redis depending on what env I am in. Can this be done from the command line?
让快递知道我在什么环境中的最简单方法是什么?例如,我想做以下操作,根据我所在的环境与 redis 建立连接。这可以从命令行完成吗?
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
var r = require("redis").createClient(6379,'127.0.0.1');
});
app.configure('production', function(){
app.use(express.errorHandler());
r = redis.createClient(6379,'46.137.195.230', { detect_buffers: true });
});
采纳答案by alessioalex
Your approach is ok, but you can make something more generic, like storing the config data for Redis in a file or passing the host and port like arguments:
你的方法没问题,但你可以做一些更通用的事情,比如将 Redis 的配置数据存储在一个文件中,或者像参数一样传递主机和端口:
node app.js REDIS_HOST REDIS_PORT
node app.js REDIS_HOST REDIS_PORT
Then in your app you can grab them using process.argv:
然后在您的应用程序中,您可以使用 process.argv 获取它们:
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
var r = require("redis").createClient(process.argv[2], process.argv[3]);
});
app.configure('production', function(){
app.use(express.errorHandler());
var r = require("redis").createClient(process.argv[2], process.argv[3], { detect_buffers: true });
});
Update:
更新:
Express will know in what environment you're in by looking at the NODE_ENV variable (process.env.NODE_ENV): https://github.com/visionmedia/express/blob/master/lib/application.js#L55
Express 将通过查看 NODE_ENV 变量(process.env.NODE_ENV)来了解您所处的环境:https: //github.com/visionmedia/express/blob/master/lib/application.js#L55
You can set that variable when starting the app like so: NODE_ENV=production node app.js(recommended), setting process.env.NODE_ENV manually in your node app before the Express code or putting that env var in ~/.profile like Ricardo said.
您可以在启动应用程序时设置该变量,如下所示:(NODE_ENV=production node app.js推荐),在 Express 代码之前在您的节点应用程序中手动设置 process.env.NODE_ENV 或将该环境变量放入 ~/.profile 中,如 Ricardo 所说。
回答by Sean
To expand on the idea of using a config.json file:
扩展使用 config.json 文件的想法:
// config.json
{
"development": {
"redisPort": 6379,
"redisHost": "127.0.0.1",
"errorHandlerOptions": {"dumpExceptions": true, "showStack": true}
},
"production": {
"redisPort": 6379,
"redisHost": "46.137.195.230",
"errorHandlerOptions": {"dumpExceptions": false, "showStack": false}
}
}
Load the config file and switch based on env.
加载配置文件并根据 env 进行切换。
// app.js
var config = require('./config.json')[app.get('env')];
app.use(express.errorHandler(config.errorHandlerOptions));
var r = require("redis").createClient(config.redisPort,config.redisHost);
Make sure the NODE_ENV is set on each server (see other answers, one way: NODE_ENV=production node app.js), and this way the config variable has the settings appropriate to the server it runs on.
确保在每台服务器上都设置了 NODE_ENV(请参阅其他答案,一种方式:)NODE_ENV=production node app.js,这样配置变量的设置就适合它运行的服务器。
回答by Ricardo Tomasi
Just set the NODE_ENVenvironment variable to productionor development, as seen in express' docs: http://expressjs.com/guide.html#configuration
只需将NODE_ENV环境变量设置为productionor development,如 express' 文档中所示:http: //expressjs.com/guide.html#configuration
I just leave NODE_ENV=developmentin the dev machine's ~/.profile(.bashrc or bash_profile on linux), and do the same for productionones.
我只是留NODE_ENV=development在开发机器的~/.profile(Linux 上的.bashrc 或 bash_profile)中,并对它们做同样的production事情。
回答by Arnaud Rinquin
I did somthing even more comprehensive by ordering the sources of such parameters :
通过对这些参数的来源进行排序,我做了更全面的事情:
var env = process.argv[2] || process.env.NODE_ENV || 'development'
var mongourl = process.argv[3] || process.env.NODE_DB || 'mongodb://localhost/default'
var port = process.env.PORT || 9001
This way you can use command line args, env settings and default values.
这样您就可以使用命令行参数、环境设置和默认值。
回答by Ashutosh Jha
Use config module that allows to create multiple config files https://www.npmjs.com/package/config
使用允许创建多个配置文件的配置模块 https://www.npmjs.com/package/config
Eg default.json , production.json
例如 default.json , production.json
And start your server wIth
并启动您的服务器
export set NODE_ENV=production && npm start
导出集 NODE_ENV=production && npm start

