Search code examples
javascriptnode.jsazureexpresswebstorm

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.


Solution

  • 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:

    setting Node config

    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!