English 中文(简体)
应用组合中的开发与生产
原标题:Node.js, express, and using development versus production in app.configure

表达我所处环境的最简单的方法是什么?例如,我想做以下工作,根据我所处的环境进行连线。这能否从命令线进行?

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 });
});
最佳回答

您的方法是确定的, 但是您可以做一些更通用的东西, 比如将 Redis 的配置数据存储在文件中, 或者通过主机和端口的参数 :

node app.js REDIS_HOST REDIS_port (代码)

然后在您的应用程序中, 您可以使用 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 });
});

更新 :

通过查看 NODE_ENV 变量( process.env.NODE_ENV): https://github.com/visionmedia/polication/blob/master/lib/application.js#L55 , Express将知道您在什么环境中看到 NODE_ENV 变量( process.env.NOde_ENV):

问题回答

要扩展使用 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}
  }
}

以 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);

确保在每个服务器上设置 NODE_ENV (参见其它答案, 一种方式 : NODE_ ENV= producation node app.js ), 这样配置变量就拥有适合其运行的服务器的设置 。

NODE_ENV 环境变量设置为 production development 环境变量,具体做法如下: > http://poreserjs.com/guide.html# configation

我刚刚离开dev机器s NODE_ENV=development (.bashrc 或bash_profile on linux)中的 NODE_ENV=development 中,对 producturing 也这样做。

我订定了这些参数的来源,

    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

这样您就可以使用命令行 args、 env 设置和默认值 。

Use config module that allows to create multiple config files https://www.npmjs.com/package/config

Eg 默认.json, production.json

启动您的服务器 wIth

导出设置 NODE_ENV= producting & amp; & amp; npm 启动





相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...