Verify if my node.js instance is dev or production
Asked Answered
U

4

105

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

Utopia answered 22/5, 2012 at 1:4 Comment(2)
Is your node.js app an HTTP server?Volscian
Yes it is. Also has a connection to mongodb.Utopia
D
184

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 || 'development';
loadConfigFile(env + '.json', doStuff);

Resources:

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

Daniel answered 22/5, 2012 at 9:27 Comment(4)
In Express 3.x and 4.x, the env variable is set by express itself here to development.Hexamethylenetetramine
In modern versions of raw node (without express), this value is also set to development. Can you update this snippet to reflect the new default?Timeless
The permalink github.com/expressjs/express/blob/… expressConvergence
I don't want to be picky, but using '||' here is the number 1 security breach!Amieva
S
19

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.

Streptomycin answered 22/5, 2012 at 3:12 Comment(1)
+1 This allows you to create multiple setups, from dev to stage to production.Microseism
B
10

For that you can just check it out with the help of environment variables and you can perform any actions.

if (process.env.NODE_ENV === "production") {
  //do something in production
}

if (process.env.NODE_ENV == "development") {
   //do something in development
}
Bag answered 29/3, 2022 at 13:52 Comment(1)
How to print in terminal to show whether node env is in development or production?Bengal
D
9

Actually, I would not recommend to store configuration values like database connection information, passwords, access tokens and such inside of actual application code for the following reasons:

  1. Hardcoding those values make it difficult to change them later on. You will have to release a new version of the application to change those values.

  2. This is a serious security violation, because production-grade configuration data and passwords shouldn't be stored in code. It's very easy to leak this sensitive data.

The better approach would be to externalize this data and pass it to your application during execution. This is normally done by means of environment variables. You just need to define unique environment variable for each peace of data that needs to be changeable between different environments.

For example: DB_HOST, DB_USER, DB_PASSWORD. Then you could pass those values to you app in production this way:

$ NODE_ENV=production DB_HOST=1.2.3.4 DB_USER=someusername DB_PASSWORD=somerandompassword /bin/node app.js

Actually, this values could be encrypted and added to the codebase and then decrypted during the deployment. However, make sure that decryption key is stored securely in deployment system or provided interactively by the release engineer. Shippable allows to do this out of the box.

In the development environment it gets simpler, because you can use very convenient dotenv module. Just create a .env file in your project's root directory and add all variables to it:

DB_HOST=1.2.3.4
DB_USER=someusername
DB_PASSWORD=somerandompassword

But, make sure to exclude it from you VCS, because each developer probably would want to have personal configuration. You can create a .env.dist file to contain default configuration, which later could be used as a template: cp .env.dist .env.

Dorcasdorcea answered 27/9, 2017 at 8:7 Comment(1)
A ubuntu (most used OS) with an disabled authentication (login/password) would do the job? secure copy scp the .env via ssh to your vps and the secure environment variables are pretty save?Pasteurizer

© 2022 - 2024 — McMap. All rights reserved.