Running node.js in production.

Node.js is my new love !!

In one of my latest projects I needed to set up a RESTFUL web service which in a nutshell would be a simple interface / API to a bunch of functions based tightly on the HTML standard commands GET, PUT, DELETE, etc..

I decided to go with a language I know JAVA ..  I never worked before with J2EE a week and bulky installations etc later , multiple mixed information , different frameworks etc.. it’sa nightmare. If you join a team who already works in a given way etc.. it’s not too bad but the divergence required to maintain and test the services, the slowness of having to run upload , connect to glassfish / tomcat , the overheads…  Well not a good experience .. got it working but sustaining it for growth and even just changing a DB config is a messy thing unless you have all the best practices in mind when you start it’s not a nice place ..

I tried other stuff including ruby and then I tried node.js … BINGO !!!!

What took me 12 hours to get going from scratch on my system, took only 5 minutes or so with node, npm and express..

Love at first sight !! Thought I’d share some of the love here !!!

Advantages :

  1. Great performance (also see 2)
  2. Designed with non-blocking architecture from the ground up
  3. Built on the Google v8 JavaScript run time engine.
  4. Ultimate transparency of code
  5. Cross platform compatible ( and one language fits all Server -> )
  6. Super libraries (many of which quite well hardened.
  7. Massive industry backing and support :
    1. Google
    2. Ebay
    3. Amazon
    4. Yahoo
    5. Microsoft (Azure)
    6. Miscellaneous but all true : https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node
    7. Massive industry support : http://nodejs.org/industry/
  8. Scales almost transparently.
  9. Support for almost all know DBs especially friendly with the new NoSQL systems (they kind of grew together)
  10. It also handles the persistence across reboots.
  11. Very concise and powerful structure.

Disadvantages :

  1. No good open source IDE with intelligent smartfill etc.. (I still use VIM and Notepad ++ )  I want a Visualstudio.js
  2. Single threaded by default though thanks to pm2 (see later) it can be an advantage.
  3. Code can become messy fast.
  4. Usual JavaScript nags –In particular I hate type variant languages.

The first question I had was how to have it work on a server

There is a number of ways to keep a node.js application running if it crashes with automatic restart. 5 of which are :

  • node-dev
  • nodemon
  • forever
  • supervisor
  • pm2

The first four are mostly a great way to run code in dev mode so when code changes they automatically reload an app and restart everything that needs to be restarted.

For production though, pm2 rules them all.

On a prod server you do not want the app to reload on a file change as soon as the file changed so that’s the first point.

The added features pm2 comes with are amazing though:

  1. Simple to run an application on n threads e.g.:
        • pm2 start app.js -i 4
  2. Logs are, by default all piped to common logs by application instance name and can be monitored from command line
        • pm2 logs app
  3. Pm2 provides a great tool to monitor the run time engines
        • pm2 monit

 

PM2 Notes can be found here: https://github.com/Unitech/pm2/blob/master/README.md

Simply awesome !!

I need to dedicate a whole section of the blog to Node !!!

nJoy 😉

 

Write a Reply or Comment

Your email address will not be published.