Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify if my node.js instance is dev or production

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

like image 202
guiomie Avatar asked May 22 '12 01:05

guiomie


People also ask

How do you know if node is in production?

Node. js assumes it's always running in a development environment. You can signal Node. js that you are running in production by setting the NODE_ENV=production environment variable.

What version of node is used in production?

However, I would abide by the versioning standard provided. V4. 2.4 is LTS (Long Term Support), and is listed as being "Mature and Dependable", while v5. 3.0 Stable is listed with "Latest Features" on the downloads for each version at the official nodejs.org site, at the time of this comment.

What is Node JS Dev?

Node.js is a free, open-sourced, cross-platform JavaScript run-time environment that lets developers write command line tools and server-side scripts outside of a browser. Download Node (LTS)

Which development tool is installed with node js?

Keystone. If you are looking for the easiest ways to learn and start developing applications with Node. js, then Keystone is the perfect place for you. Keystone, based on Express, is an open source and full stack framework.


2 Answers

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?

like image 146
alessioalex Avatar answered Sep 20 '22 19:09

alessioalex


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.

like image 42
Vadim Baryshev Avatar answered Sep 19 '22 19:09

Vadim Baryshev