Verify if my node.js instance is dev or production


Question

Right now, whenever I want to deploy a node.js server to my production server, I need to change all the IP/DNS/username/password for my various connection to my databases and external APIs.

This process is annoying, is there a way to verify if the currently running node.js instance is in cloud9ide or actually my production joyent smartmachine?

If I am able to detemrine (in my running code) on which server my node.js instance is running , I'll add a condition that set the values to the prod or dev.

Thank you

1
51
5/22/2012 1:04:52 AM

Accepted Answer

Normally you should run a node app in production like this:

NODE_ENV=production node app.js

Applications with Express, Socket.IO and other use process.env.NODE_ENV to figure out the environment.

In development you can omit that and just run the app normally with node app.js.

You can detect the environment in your code like this:

var env = process.env.NODE_ENV || 'dev';
loadConfigFile(env + '.json', doStuff);

Resources:

How do you detect the environment in an express.js app?

79
5/23/2017 10:31:24 AM

I think the easiest way to set the environment is to pass command-line argument to your application.

node ./server.js dev

In your script you need to handle this argument and set configuration what you need for it.

var env = process.argv[2] || 'dev';
switch (env) {
    case 'dev':
        // Setup development config
        break;
    case 'prod':
        // Setup production config
        break;
}

Also, i was created module that makes the configuration process a bit easier. Maybe it will help you.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon