简体   繁体   中英

Configure Node.js app based on machine type or IDE flags

I want to be able to do different things on my server when running on a web server or running on localhost, like disabling logs or connecting to different data stores.

Is there any way to configure a Node.js app differently based on some IDE definitions (specifically WebStorm in this case) or identify the machine a Node.js express server is running on - and more specifically detecting if the server is running on Azure or on localhost.

The standard way to do this in Node is to use an environment variable that identifies each unique deployment config, combined with custom settings that map to each of those configs:

Here's a good SO topic on that:

environment configs in Node

Combined with the above, in Azure you would define the NODE_ENV variable in Application settings to denote that you're running in Azure, something like this:

设置节点配置

In your code you check the value of process.env.NODE_ENV and use the appropriate action based on whether you're in Azure, on localhost, or some other environment as appropriate for your needs.

Here's a small code sample that demonstrates further:

github sample

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM