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 :
- Great performance (also see 2)
- Designed with non-blocking architecture from the ground up
- Built on the Google v8 JavaScript run time engine.
- Ultimate transparency of code
- Cross platform compatible ( and one language fits all Server -> )
- Super libraries (many of which quite well hardened.
- Massive industry backing and support :
- Ebay
- Amazon
- Yahoo
- Microsoft (Azure)
- Miscellaneous but all true : https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node
- Massive industry support : http://nodejs.org/industry/
- Scales almost transparently.
- Support for almost all know DBs especially friendly with the new NoSQL systems (they kind of grew together)
- It also handles the persistence across reboots.
- Very concise and powerful structure.
Disadvantages :
- No good open source IDE with intelligent smartfill etc.. (I still use VIM and Notepad ++ ) I want a Visualstudio.js
- Single threaded by default though thanks to pm2 (see later) it can be an advantage.
- Code can become messy fast.
- 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:
- Simple to run an application on n threads e.g.:
- pm2 start app.js -i 4
- Logs are, by default all piped to common logs by application instance name and can be monitored from command line
- pm2 logs app
- 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 😉