NODE_ENV在快递里是什么?

时间:2021-05-14 23:10:13
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的事情。我需要把它添加到这个代码吗?怎么加呢?

2 个解决方案

#1


261  

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.

NODE_ENV是一个由express webserver框架流行的环境变量。当运行节点应用程序时,它可以检查环境变量的值,并根据值做不同的事情。NODE_ENV(按照约定)专门用于说明特定环境是生产环境还是开发环境。一个常见的用例是在开发环境中运行额外的调试或日志代码。

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

var = process.env.NODE_ENV环境

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

或者也可以使用express' app.get('env')(注意:这默认为“开发”)

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

请注意,如果您没有为您的环境显式设置NODE_ENV,那么它将是未定义的。

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
  • linux和mac: export NODE_ENV=production
  • windows: set NODE_ENV=production
  • windows:设置NODE_ENV =生产

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:

按照惯例,NODE_ENV应该使用的值只有两个,要么是产品,要么是开发,都是小写的。没有什么能阻止你添加更多的值,但这可能不是一个好主意,因为我在我使用的许多node_modules中看到了很多这样的代码:

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.

注意,尝试在节点应用程序本身中设置NODE_ENV是一个非常糟糕的想法——如果这样做,它将只适用于设置节点的进程,因此事情可能不会像您期望的那样工作。别这么做——你会后悔的。

#2


7  

OK,

好吧,

NODE_ENV stands for environment variable in express server ...

NODE_ENV在express server中表示环境变量……

It's how we set and detect which environment we are in...

这是我们如何设置和检测我们所处的环境。

It's very common using production and development...

使用生产和开发是很常见的……

Set:

设置:

export NODE_ENV=production

Get:

得到:

You can get it using app.get('env')

可以使用app.get('env')获取

#1


261  

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.

NODE_ENV是一个由express webserver框架流行的环境变量。当运行节点应用程序时,它可以检查环境变量的值,并根据值做不同的事情。NODE_ENV(按照约定)专门用于说明特定环境是生产环境还是开发环境。一个常见的用例是在开发环境中运行额外的调试或日志代码。

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

var = process.env.NODE_ENV环境

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

或者也可以使用express' app.get('env')(注意:这默认为“开发”)

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

请注意,如果您没有为您的环境显式设置NODE_ENV,那么它将是未定义的。

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
  • linux和mac: export NODE_ENV=production
  • windows: set NODE_ENV=production
  • windows:设置NODE_ENV =生产

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:

按照惯例,NODE_ENV应该使用的值只有两个,要么是产品,要么是开发,都是小写的。没有什么能阻止你添加更多的值,但这可能不是一个好主意,因为我在我使用的许多node_modules中看到了很多这样的代码:

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.

注意,尝试在节点应用程序本身中设置NODE_ENV是一个非常糟糕的想法——如果这样做,它将只适用于设置节点的进程,因此事情可能不会像您期望的那样工作。别这么做——你会后悔的。

#2


7  

OK,

好吧,

NODE_ENV stands for environment variable in express server ...

NODE_ENV在express server中表示环境变量……

It's how we set and detect which environment we are in...

这是我们如何设置和检测我们所处的环境。

It's very common using production and development...

使用生产和开发是很常见的……

Set:

设置:

export NODE_ENV=production

Get:

得到:

You can get it using app.get('env')

可以使用app.get('env')获取