Javascript 你如何检测 express.js 应用程序中的环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8449665/
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
How do you detect the environment in an express.js app?
提问by sethvargo
How do you detect what environment an expressJS app is running in? (development, test, production?). There's nothing in process.env
indicating an environment...
你如何检测 expressJS 应用程序运行在什么环境中?(开发、测试、生产?)。没有什么可以process.env
指示环境...
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...
我知道您可以在每个环境下的配置文件中声明变量,但是如果您动态加载模块,这无济于事...
回答by alessioalex
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).
您可以通过检查app.settings.env
(这将在 Express 中工作)来检查环境,也可以通过检查以更直接的方式进行检查process.env.NODE_ENV
(环境是在该变量中找到的环境或默认情况下的“开发”<这也适用于其他库,如 Socket.IO 等)。
回答by DB Prasad
app.get('env') would also return the environment.
app.get('env') 也会返回环境。
if ( app.get('env') === 'development' ) {
app.use(express.errorHandler());
}
回答by iamwhitebox
I'd like to address a straightforward way to passing NODE_ENV variables to your node script in order to access them in process.env
我想解决一种将 NODE_ENV 变量传递给节点脚本的直接方法,以便在 process.env
"scripts": {
"start": "./node_modules/.bin/cross-env NODE_ENV=development ./node_modules/.bin/nodemon server.js",
"debug": "./node_modules/.bin/cross-env NODE_ENV=development ./node_modules/.bin/nodemon --debug server.js",
"test": "./node_modules/.bin/cross-env NODE_ENV=test ./node_modules/.bin/babel-tape-runner test/test-*.js"
},
can be used as
可以用作
if ( app.get('env') === 'development' ) {
app.use(express.errorHandler());
}
回答by DHamrick
The can detect which environment you are in by inspecting app.settings.env
.
可以通过检查来检测您所处的环境app.settings.env
。
回答by ZILONG PAN
cannot access the nodejs server. can detect node env from express using app.setting.env
无法访问 nodejs 服务器。可以使用 app.setting.env 从 express 中检测节点 env
var app = express();
- app.setting.env render to the template engine.
- check from the browser.
var app = express();
- app.setting.env 渲染到模板引擎。
- 从浏览器检查。
回答by Slava Fomin II
There are a lot of useful recommendations in other answers. I'm generally doing it like this:
其他答案中有很多有用的建议。我一般是这样做的:
const environment = process.env.NODE_ENV || 'development';
The good thing is that such approach is not specific to Express per se, but actually is an accepted practice in wider Node.js ecosystem.
好消息是,这种方法本身并不特定于 Express,但实际上是更广泛的 Node.js 生态系统中公认的做法。
Also, I've implemented a reusable module, which allows to automatically detect environment by analyzing both CLI arguments and NODE_ENV variable. This could be useful on your development machine, because you can easily change environment by passing a CLI argument to you Node.js program like this: $ node app.js --prod
.
此外,我还实现了一个可重用模块,它允许通过分析 CLI 参数和 NODE_ENV 变量来自动检测环境。这可能是在开发机器上是有用的,因为你可以通过一个命令行参数传递给你Node.js加载这样的程序轻易改变环境:$ node app.js --prod
。
Please see more details and use cases on the detect-environment
's page.
请在detect-environment
的页面上查看更多详细信息和用例。