I have just finished a contract and for the last 4 years I have been doing infrastructure work and team team building. A lot has changed in the software development space and one technology interesting technology is node.js.
The big ideas in node.js
You don’t need a “proper” web server
Unlike a typical application server such as Tomcat or JBOSS you don’t put a web server front end on it.
Only One Thread
Proper web servers will allocate a thread to each request so big sites like FaceBook or a small device like a Raspberry Pi quickly run out of resources. node.js only has one thread that processes all requests. It does this by being event driven and using Callbacks.
It uses Callback functions
For example, suppose we wanted to fetch some data, in the PHP world we might execute processData() and we would then have to wait for the data before we could continue execution. In node.js we would execute processData(callback) we can then continue execution in the knowledge that when processing finishes the callback function will run. (The source for this example discusses this at greater length.)
It has thousands of modules
node.js comes with a library manager called “npm”
node.js is easy and hard
Experienced programmers will find node.js very easy. You just download a lightweight executable to your PC, write a couple of files and you will have a working “Hello World” application. Less experienced web developers might find it a big step up from PHP (say) because you have to think about http transport and asynchronous programming.
A good resource to learn node.js is openclassrooms.com. Their 15 hour course took me about 2 hours to read. I have not done the exercises yet but I would estimate 3 hours tops. Exercises are also available at nodeschool.
node.js for desktop applications
node.js is a natural choice for cross-platform desktop applications. The electron module and toolset allows you to build desktop applications for Mac, Windows and Linux that have full access to the file system and very fast io. Slack is an example of a desktop application written in node.js.
node.js is a poor fit for mobile
node.js or WordPress?
At first sight it seems odd to compare a very low-level application server with a very high-level content management system but actually they have some common characteristics.
- They can provide the REST back end for desktop and mobile applications
- They are supported by thousands of open-source component developers (modules vs plugins)
My business goal is always to get an application in the customer’s hands with the minimum of coding effort. WordPress gives you theming, user management and navigation out-of-the box. You can hammer most requirements in the WordPress shape. If it running slowly you can just pay a few dollars for a bigger box.
So why might you choose node.js over WordPress?
Performance – Automattic the owners of WordPress choose node.js for the management interface that they give to blog owners. The front end is built with React on node.js and it interacts with the blogs through the WordPress API. This is a good example of how node.js can be used to manage high traffic websites. They describe their reasoning here.
Size and Security – WordPress is a massive install which includes a web server, application server and database with a huge attack surface. It is not a natural choice for small applications that must be secure such as embedded systems. (I note that Raspberry Pi are now so powerful performance is not actually a problem). For embedded systems where cost, security and performance are important then node.js would be the preferred option.
For most of my projects WordPress will be the favoured platform because it typically only takes a few hours to build a prototype and a few days to build a production quality product. When the traffic from a project is so large that a AWS cluster cannot deliver it I will have enough money for a rewrite in node.js.
I will actively consider node.js for embedded electronics because of its superior performance and the the availability of lower level libaries for io (Raspberry Pi).