如何在一个express.js应用程序中检测环境? [英] How do you detect the environment in an express.js app?

查看:119
本文介绍了如何在一个express.js应用程序中检测环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测expressJS应用程序运行的环境? (开发,测试,生产?)。 process.env 中没有指示环境...

How do you detect what environment an expressJS app is running in? (development, test, production?). There's nothing in process.env indicating an environment...

我知道您可以声明变量你在每个环境下的配置文件,但是如果你正在动态加载模块,这不是有帮助的。

I'm aware that you can declare variables in your configuration file under each environment, but that doesn't help if you are dynamically loading modules...

推荐答案

通过检查 app.settings.env (这将在Express中使用),或者您可以通过检查以更直接的方式来检查环境process.env.NODE_ENV (环境是该变量中的一个或默认情况下是开发),这也适用于其他库,如Socket.IO等)。

You can either check the environment by checking the app.settings.env (this will work in Express), or you can do it in a more direct way by checking process.env.NODE_ENV (the environment is the one found in that variable or 'development' by default < this also works in other libraries such as Socket.IO etc).

这篇关于如何在一个express.js应用程序中检测环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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