node.js 什么是 NODE_ENV 以及如何在 Express 中使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16978256/
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
What is NODE_ENV and how to use it in Express?
提问by TIMEX
This is my the app, I'm currently running on production.
这是我的应用程序,我目前正在生产中运行。
var app = express();
app.set('views',settings.c.WEB_PATH + '/public/templates');
app.set('view engine','ejs');
app.configure(function(){
app.use(express.favicon());
app.use(express.static(settings.c.WEB_PATH + '/public'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.session({
cookie:{ domain:"."+settings.c.SITE_DOMAIN, maxAge:1440009999},
secret:'hamster',
store: r_store,
}));
app.use(useragent.express());
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
});
However, I came to know about NODE_ENVand want to use it. How can I do this?
但是,我开始了解NODE_ENV并想使用它。我怎样才能做到这一点?
回答by Ed Hinchliffe
NODE_ENVis an environment variablemade popular by the expresswebserver framework. When a node application is run, it can check the value of the environment variable and do different things based on the value. NODE_ENVspecifically is used (by convention) to state whether a particular environment is a productionor a developmentenvironment. A common use-case is running additional debugging or logging code if running in a development environment.
NODE_ENV是由express网络服务器框架流行的环境变量。当一个节点应用程序运行时,它可以检查环境变量的值并根据该值做不同的事情。特别是(按照惯例)用于说明特定环境是生产环境还是开发环境。如果在开发环境中运行,一个常见的用例是运行额外的调试或日志记录代码。NODE_ENV
Accessing NODE_ENV
访问 NODE_ENV
You can use the following code to access the environment variable yourself so that you can perform your own checks and logic:
您可以使用以下代码自己访问环境变量,以便您可以执行自己的检查和逻辑:
var environment = process.env.NODE_ENV
var environment = process.env.NODE_ENV
Or alternatively using express' app.get('env')(note:this defaults to "development")
或者也可以使用 express' app.get('env')(注意:默认为"development")
Be aware that if you haven't explicitly set NODE_ENVfor your environment, it will be undefined.
请注意,如果您没有NODE_ENV为您的环境明确设置,它将是undefined.
Setting NODE_ENV
设置 NODE_ENV
How to actually set the environment variable varies from operating system to operating system, and also depend on your user setup.
如何实际设置环境变量因操作系统而异,也取决于您的用户设置。
If you want to set the environment variable as a one-off, you can do so from the command line:
如果要将环境变量设置为一次性的,可以从命令行执行此操作:
- linux & mac:
export NODE_ENV=production - windows:
$env:NODE_ENV = 'production'
- linux & mac:
export NODE_ENV=production - 窗户:
$env:NODE_ENV = 'production'
In the long term you should persist this so that it doesn't unset if you reboot - rather than list all the possible methods to do this, I'll let you search how to do that yourself!
从长远来看,您应该坚持这一点,以便在您重新启动时不会取消设置 - 而不是列出所有可能的方法来做到这一点,我会让您自己搜索如何做到这一点!
Convention has dictacted that there are only two values you should use for NODE_ENV, either productionor development, all lowercase. There's nothing stopping you adding more values, but it's probably not a good idea, as I see a lot of this sort of code in many of the node_modules that I use:
公约dictacted有只有两个你应该使用值NODE_ENV,无论是production或development,全部小写。没有什么可以阻止您添加更多值,但这可能不是一个好主意,因为我在我使用的许多 node_modules 中看到了很多此类代码:
var development = process.env.NODE_ENV !== 'production';
Note that it's a really badidea to try to set NODE_ENVfrom withina node application itself- if you do it will only apply to the process from which it was set, so things probably won't work like you expect them to. Don't do it - you'll regret it.
请注意,这是一个非常糟糕的主意,试图一套NODE_ENV从内节点应用程序本身-如果你这样做将只适用于从它被设置的过程中,这样的事情可能不会像工作,你希望他们。不要这样做 - 你会后悔的。
回答by Alireza
NODE_ENVis an environmental variable that stands for node environmentin express server.
NODE_ENV是一个环境变量,代表express 服务器中的节点环境。
It's how we set and detect which environment we are in.
这就是我们如何设置和检测我们所处的环境。
It's very common using productionand development.
使用productionand很常见development。
Set:
放:
export NODE_ENV=production
Get:
得到:
You can get it using app.get('env')
你可以得到它使用 app.get('env')
回答by Randy Hudson
I assume the original question included how does Express use this environment variable.
我假设最初的问题包括 Express 如何使用这个环境变量。
Express uses NODE_ENV to alter its own default behavior. For example, in development mode, the default error handler will send back a stacktrace to the browser. In production mode, the response is simply Internal Server Error, to avoid leaking implementation details to the world.
Express 使用 NODE_ENV 来改变它自己的默认行为。例如,在开发模式下,默认错误处理程序会将堆栈跟踪发送回浏览器。在生产模式下,响应很简单Internal Server Error,以避免将实现细节泄露给全世界。

