What I Love About Node.js

I am in love with the technology and its name is Node.js. For the uninitiated, Node.js is Google's super fast V8 JavaScript engine, which has been extracted from Chrome, to be able to be used as a general purpose scripting language, particularly for server-side web development.

I think the first obvious thing of interest about Node.js is the ability to use the same language on the client and server when developing a web app. The web is already such a hodgepodge of languages and technologies that anything that can be done to reduce the number of languages and promote code reuse is a major help in reducing this complexity. Another great aspect of this is that you don't have to do a lot of type conversion between the client and server. You can use JSON as the serialized format and it's actually the native format on both the client and server.

Node.js is fairly minimal and doesn't prescribe any particular way a web application should look with one exception. Node.js is non-blocking and event-driven. If you look at JavaScript as being an unopinionated language, then Node.js asserts a single opinion: asynchronous code increases performance. All I/O (e.g., disk, networking), which would normally block the execution of a script, is handled asynchronously by Node.js and requires a callback. Some people seem to find this the hardest thing to adjust to, but you get used to it and it has a major positive effect on performance.

It cannot be emphasized enough just how fast and lightweight Node.js is. Part of this speed can be attributed to just how crazy optimized the V8 engine is and part can be attributed to the use of asynchronous programming.

Adding I/O to JavaScript required some additions to the language, but Node.js still keeps it simple by adding relatively few things. Compare this to PHP which has a huge standard API, which mostly exists as thousands of long function names littering up the global namespace.

Node.js may not have a large standard library, but what it does have is a fantastic package manager. Node.js's package manager, NPM, has a lot of good stuff in it and does a great job of handling versioning and dependencies. NPM has a thriving community of contributors, which is certainly a vote of confidence in Node.js's future. Some Node.js modules I like are:

  • Express - web application framework
  • Jade - template engine
  • Socket.IO - HTML5 WebSocket implementation
  • Passport - authentication supporting many different strategies (e.g., Facebook and Twitter)

Node.js isn't just for web serving, you can actually implement any kind of server with it. This makes it a great solution to implement things like WebSockets or servers for games. You can, of course, also just use it as a general purpose scripting language to create command-line tools or anything else.

Node.js has a lot of things going for it and is now my go-to choice for creating web applications. This very blog is a Node.js application (shocking, I know).

Posted on Nov 14, 2012
Written by Emlyn Murphy