A Time and a Place: When To Use Node.js
Before committing myself full-time to Node.js almost two years ago I would have considered myself the jack-of-all-trades web developer. I wrote most of my applications in some flavor of PHP or Python. Experimented with Java and Ruby on the weekends. Also, like a lot JavaScript developers at the time, I mostly wrote plugins for jQuery. In retrospect, I was the unfocused developer still waiting for something to tie it all together.
When I first tried Node.js it was like a breath of fresh air. I could write the entire application in one language. Also, the early tools from the developer community made it easy to build discrete, small and testable modules. Though modules are a foundation of the other environments, Node.js and npm (Node’s command-line dependency manager) seemed to address the many things I disliked about those systems.
The Node.js tools and community have grown since then, but the core philosophies are still there. Most importantly, there are many ways to approach solving any particular problem. In my previous article Getting Started With Node.js I discussed what Node.js is, where it came from, and how early enterprise adopters are using it. In this piece I hope to inspire you to take a further look at Node.js and consider it for your next project.
A Growing Talent Pool
So, you have committed to a new project. Maybe you are the developer and realize that your current tool belt does not satisfy all your needs, or perhaps you just want to learn something new. Alternatively, you are the startup mastermind preparing the job description to attract a “rockstar” developer or technical cofounder to your small but growing team.
There are many different criteria to examine when planning a new endeavor, but none of them as important as talent. There is a lot of talent in the JavaScript space because it is so ubiquitous, but Node.js is a new tool. It seems almost paradoxical until you realize that it is the perfect time to seek out talent. There is a fairly substantial pool of potential hires and all of them interested in the bleeding edge. These candidates are the independent thinkers and self-taught coders who want to work in startups solving the challenging problems no one has addressed before.
The Union of Client and Server
If you are a client-side JavaScript developer, now is the time to boot up your first Node.js server. You are already at an advantage: you don’t have to relearn everything you know. You can now work on both sides of the fence.
There are many tools available on npm that make it easy to get up and running quickly. A good starting point would be the Express web application framework and Mongoose ODM for data modelling with MongoDB. They impose very little structure or organization on the project and give the developer free reign to explore and implement a style that is most comfortable. Furthermore, if you are a self-taught newcomer to Node.js and asynchronous programming, these tools prove to be extremely valuable resources.
However you are looking for more structure, Model-View-Controller (MVC) is likely the most popular server-side web development framework paradigm in existence. Most languages have one or many popular frameworks that implement the MVC philosophy of separating presentation from data modelling. Developers use this method of organization for its versatility, debugging and collaborative ease. As web application demanded richer user interfaces and JavaScript took the spotlight the demand for JavaScript developers grew with it. Today there are many client-side Javascript frameworks that implement a derivation of MVC such as Backbone.js.
Even if you’re a developer who knows very little JavaScript but are familiar with the MVC paradigm you should consider exploring one of the many frameworks available for or built on-top of Node.js. Though not strictly an MVC, Meteor.js is one such tool that boasts JavaScript on both the client and server side. JavaScript developers new to server-side development will be able to adapt quickly. Developers coming from Rails or similar frameworks will find familiarity in the way code is organized and power in the way it executed.
Getting the Most from Multi-Core Hardware
Some applications, particularly in the enterprise, require the high memory capacity or multi-threading of the Java Virtual Machine (JVM). The JVM offers quite an advantage on modern hardware with multiple core processors. Because of this, some developers shy away from Node.js once they discover that it does not inherently support multi-threading; A single Node.js process is a single thread.
Node.js has a time and a place. One scenario to examine is a web or RESTful application which requires support for a large number of concurrent requests. Included in Node.js is the “cluster” module which will allow any web server process to be forked into multiple processes all sharing the same port. It is then left to the operating system to handle load-balancing. Those interested in learning more should visit the Node.js Cluster API Documentation.
The other scenario that usually warrants increasingly complex systems is long running utilities. While Node.js’ asynchronous model shows performance gains when reading and writing databases, making requests to third-party APIs and doing filesystem IO, not all operations belong in the same thread. Complex operations such as encoding media have no place in the same process as the web server. Rails applications solved this issue long ago with background queues and Node.js has followed suit. Tools such as Kinetik are invaluable in complex applications. Some Rails developers have begun to migrate their background processing to Node.js for increased throughput.
Closing Remarks
Client-side engineers have a number of modules at their disposal to make the transition to Server-Side JavaScript a smooth one. Those who use the MVC paradigm will find familiarity in similar tools for Node.js. Enterprise users addicted to the JVM should reevaluate their use of it, since it might be time to try something lighter. Perhaps the same performance can be accomplished on a lighter application stack and less servers, especially if clustering and background queues can handle your workload.
Choosing the right tools is vital and so is choosing the right talent. So as you plan your next big web application, interview your next hire, or commit to a weekend to a learning a new skill, ask yourself what you hope to accomplish. Hopefully the answer is that now is the perfect time to give Node.js a try!
Resources
- Node.js (GitHub: joyent/node)</li>
- npm Registry</li>
- Getting Started with Node.js</li>
- Express</li>
- Node.js Cluster API Documentation</li>
- Kinetik</li>
Share your thoughts with @engineyard on Twitter