Express中的NODE_ENV是什么? [英] What is NODE_ENV in Express?

查看:135
本文介绍了Express中的NODE_ENV是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:
$ b app.use(useragent.express());
app.use(flash());
app.use(passport.initialize( ));
app.use(passport.session());
});

这是我的应用程序。我目前正在生产中运行。



然而,有人告诉我关于 NODE_ENV 。我必须将其添加到此代码?如何添加?

解决方案

NODE_ENV 是一个环境变量 express webserver框架所流行。当运行节点应用程序时,它可以检查环境变量的值,并根据该值执行不同的操作。特定地使用(通过惯例) NODE_ENV 来说明特定环境是否是生产开发环境。一个常见的用例是在开发环境中运行时运行附加的调试或记录代码。



访问NODE_ENV



您可以使用以下代码自行访问环境变量,以便您可以执行自己的检查和逻辑:



var environment = process。 env.NODE_ENV



或者使用express' app.get('env')注意),默认为development



意识到如果您没有为您的环境明确设置 NODE_ENV ,那么它将是 undefined / p>

设置NODE_ENV



如何实际设置环境变量因操作系统而异,并且还取决于您的用户设置。



如果要将环境变量设置为一次性,可以从命令行执行:




  • linux& mac export NODE_ENV = production

  • Windows set NODE_ENV =生产



从长远来看,您应该坚持这样做,如果你不重新启动 - 而不是列出所有可能的方法来做到这一点,我会让你搜索如何做到这一点!



约定规定,只有两个值应该使用 NODE_ENV production development ,all小写。没有什么可以阻止你添加更多的值,但这可能不是一个好主意,因为我在许多使用的node_modules中看到很多这样的代码:

  var development = process.env.NODE_ENV!=='production'; 

请注意,这是一个非常糟糕的想法,试图设置节点应用程序本身 c $ c> NODE_ENV - 如果这样做,仅适用于设置该节点应用程序的进程,所以事情可能不会像你所期望的那样工作。不要这样做 - 你会后悔的。


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());
});

This is my app. I'm currently running it in production.

However, someone told me about NODE_ENV. Do I have to add it to this code? How do I add it?

解决方案

NODE_ENV is an environment variable made popular by the express webserver 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_ENV specifically is used (by convention) to state whether a particular environment is a production or a development environment. A common use-case is running additional debugging or logging code if running in a development environment.

Accessing 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

Or alternatively using express' app.get('env') (note: this defaults to "development")

Be aware that if you haven't explicitly set NODE_ENV for your environment, it will be undefined.

Setting 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: set 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 production or 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:

var development = process.env.NODE_ENV !== 'production';

Note that it's a really bad idea to try to set NODE_ENV from within a 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.

这篇关于Express中的NODE_ENV是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆