什么是 NODE_ENV 以及如何在 Express 中使用它? [英] What is NODE_ENV and how to use it in Express?

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

问题描述

这是我目前正在生产中运行的应用.

This is my app that 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());
});

现在我已经了解了 NODE_ENV 并想使用它.我该怎么做?

Now I've learned about NODE_ENV and want to use it. How can I do this?

推荐答案

NODE_ENV 是一个 环境变量express 网络服务器框架流行起来.当一个节点应用程序运行时,它可以检查环境变量的值并根据该值做不同的事情.NODE_ENV 专门用于(按照惯例)来说明特定环境是生产 还是开发 环境.如果在开发环境中运行,一个常见用例是运行额外的调试或记录代码.

NODE_ENV is an environment variable made popular by the express web server 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.

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

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

如果您不承认价值,就假设生产:

Assume production if you don't recognise the value:

var isDevelopment = environment === 'development'

if (isDevelopment) {
  setUpMoreVerboseLogging()
}

您也可以使用 express' app.get('env') 函数,但请注意这是不推荐,因为它是默认的"development",这可能会导致开发代码在生产环境中意外运行 - 如果您的应用程序在未设置此重要值的情况下抛出错误(或者如果首选,默认为生产逻辑如上).

You can alternatively using express' app.get('env') function, but note that this is NOT RECOMMENDED as it defaults to "development", which may result in development code being accidentally run in a production environment - it's much safer if your app throws an error if this important value is not set (or if preferred, defaults to production logic as above).

请注意,如果您没有为您的环境明确设置 NODE_ENV,如果您从 undefined>process.env,没有默认值.

Be aware that if you haven't explicitly set NODE_ENV for your environment, it will be undefined if you access it from process.env, there is no default.

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

How to actually set the environment variable varies from operating system to operating system, and also depends 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:导出 NODE_ENV=production
  • windows:$env:NODE_ENV = 'production'

从长远来看,您应该坚持这一点,以便在您重新启动时不会取消设置 - 而不是列出所有可能的方法来做到这一点,我会让您自己搜索如何做到这一点!

In the long term, you should persist this so that it isn'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!

约定规定您应该为 NODE_ENV 使用两个主要"值,productiondevelopment,全部小写.没有什么可以阻止您使用其他值(例如,test,如果您希望在运行自动化测试时使用一些不同的逻辑),但请注意,如果您使用的是第三方模块,他们可能会明确地与 'production''development' 进行比较,以确定要做什么,因此可能会出现不立即明显的副作用.

Convention has dictated that there are two 'main' values you should use for NODE_ENV, either production or development, all lowercase. There's nothing to stop you from using other values, (test, for example, if you wish to use some different logic when running automated tests), but be aware that if you are using third-party modules, they may explicitly compare with 'production' or 'development' to determine what to do, so there may be side effects that aren't immediately obvious.

最后,请注意,尝试从节点应用程序本身内部设置NODE_ENV是一个非常糟糕的想法 - 如果你这样做了,它将只应用于设置它的过程,所以事情可能不会像你期望的那样工作.不要这样做 - 你会后悔的.

Finally, 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 be applied to the process from which it was set, so things probably won't work like you'd expect them to. Don't do it - you'll regret it.

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

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