Node.js - A perspective of Java Developer
November 2017
Node.js is an open-source cross-platform JavaScript runtime environment that enables us to write server-side components using JavaScript programming language. Based on Google’s V8 JavaScript Engine, it uses an event-driven, non-blocking I/O model which is ideal for data-intensive real-time applications that run across distributed devices.
Thanks to JavaScript, the way the web applications are built has dramatically changed in the recent past. It brings in a lot of capabilities and advantages (listed below) over other languages. And with Node.js things only have become an easier and minimalistic way of writing applications these days.
JavaScript is everywhere
JavaScript was created by Netscape meant for the internet and it’s been there since beginning. It is there in every browser which means available on every personal computer in the world. And with Node.js it is available on server too. Moreover, attempts to bring in alternative client based technologies such as Java Applets, Microsoft Silverlight, Adobe Flash, etc. failed to live up to the needs and slowly faded away due to complexities involved.
Non-blocking I/O model
In blocking I/O model, while one line of code is executing the rest of the code is locked waiting for the previous one to finish. In order to achieve concurrency, this model required spawning of multiple threads to process concurrent requests in parallel. However, this comes at a cost of infrastructural and architectural upgrades.
For e.g., when you run the following Java program:
Program above generates following output in the following sequence:
Start
Sleeps for 3 seconds
I am here
Sleep for 1 second
End
On the other hand, non-blocking I/O model gives each line of code a shot and then through callbacks it can come back when an event happens.
For e.g., when you run the following JavaScript program:
Program above generates following output in the following sequence:
Start
I am here
End
Timeout for 1 second
Timeout for 3 seconds
If you have noticed, “Thread sleep” was actually simulated using the “setTimeOut” function. Technically this is a callback function in JavaScript which is already executed by the interpreter but the callback function returns only once the specified timeout period is over. Traditionally, JavaScript was intended to be lightweight that runs faster on browsers, which itself is a single thread model. This is the reason why JavaScript is single thread and hence faster in execution.
Usage of JSON
JSON (JavaScript Object Notation) is simple, lightweight data exchange format that has enabled JavaScript (and Backend) developers to exchange one standard format across layers thereby making it interoperable and easy to use. JSON and JavaScript have reinforced each other's importance thereby influential in the creation of document-oriented NoSQL databases such as MongoDB and CouchDB. Content stored in these databases is JSON in format.
Productivity
With JavaScript and Node.js, the gap that existed between UI developers and Backend developers has been broken down, which means merging frontend and backend developers into one team thereby bringing lot of efficiency and transparency. Moreover, Node.js also has a great package management system called NPM; it’s the first package systems for JavaScript to get dependency management right. NPM effectively kills the possibility of experiencing dependency hell.
Community Support
The community behind JavaScript is quite vibrant and has penetrated in almost all the fields of technology: data visualization, client-side frameworks, server-side frameworks, databases, robotics, building tools and many more.
Business Cases to decide Node.js
There are few business cases where Node.js can be adapted
Internet of Things
With JavaScript being everywhere, it can be used for programming both endpoint devices and web services using Node.js. Leveraging the capabilities of HTML5, JavaScript can also be used to develop UI applications that can run literally on any device.
Real-time / Streaming Services
Due to the non-blocking I/O model, Node.js is ideal for writing network and data intensive applications. Chats, Video Streaming (leveraging HTML5 streaming capability), and other Collaboration tools are basically two-way real-time based applications. Also, Node.js does support both WebSocket and Server Sent Event protocols, which is ideal technology when it comes to implementing near real-time web applications.
Build APIs
One of the key characteristics of APIs is scalability. If your API is not I/O scalable, then it is creating bad user experience that in turn hurting your business. Yes, one can argue that scaling issues can be addressed by means of horizontal scaling, but this comes at a cost and technical debt. Due to the non-blocking I/O model paradigm, it is a great choice for building lightweight APIs that can handle a lot of requests that are basically I/O driven.
"Node.js was never created to solve the compute scaling problem. It was created to solve the I/O scaling problem"
Development Aspects
It all depends on the type of applications that you are working on. There are frameworks that are meant for specific use and one need to do careful study before adapting the same.
Node.js core ships with connectors and libraries such as HTTP, SSL, compression, filesystem access, and raw TCP/UDP, thus leveraging the capabilities of the server and the resources that it relies on. Also, with NPM (Node Package Manager), it is easy to include modules and the dependencies that are necessary to applications. Full list of packaged modules can be found https://npmjs.org/
To develop REST APIs, one can use Express, Hapi.js, Restify, Loopback, etc. And If you happen to be developing enterprise web applications that includes UI, Server side and DB then use MEAN stack, a free and open-source JavaScript stack that provides end-to-end capabilities starting from client to server to database. MEAN is an acronym for:
MongoDB
It is a leading open-source NoSQL database empowering businesses to be agile and scalable with both structured and unstructured data. It saves data in binary JSON format which makes it easier to pass data between client and server
Express
Lightweight and flexible Node.js based web based framework providing a robust set of features for building single and multi-page, and hybrid web applications
AngularJS
JavaScript based MVC framework to develop front end applications. It lets you extend HTML vocabulary by means of DOM manipulation that result in an environment which is extraordinarily expressive, readable, and quick to develop
NodeJS
Platform and runtime environment for rapid development of I/O scalable network applications
“One of the biggest advantages of using MEAN stack is that the programming language and data exchange format would remain same across architectural layers - JavaScript and JSON respectively”
With just MEAN stack one will have to invest time to understand the modules, and adaptability before usage. You can overcome such challenges and unknowns by adapting the best suitable MEAN frameworks such as:
Perhaps any framework is good for development as it provides abstraction to underlying services thereby reducing lot of boilerplate code thus allowing developers to concentrate on implementing the functional aspects alone. End result is maintainable code.
Unit Testing Aspects
Plethora of libraries is available for unit testing. Some of them follow TDD approach and some BDD. So it all depends on the architectural decisions of your projects. Listed below is some of the known unit testing frameworks:
Mocha – Can be used for both TDD and BDD approach. Ideal for standalone services and REST APIs. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases
Expresso – TDD framework written for Node.js. Expresso is extremely fast, and is packed with features such as additional assertion methods, code coverage reporting, CI support, and more
PhantomJS – For Rich UI applications that does testing using headless browsers
Build and Deployment Strategy
When it comes to deployment, the challenges are multifold as it involves packaging, dependency management, single-step deploy and hosting without service interruption. Although there was a lack of best practices around this area, some tools which are out there make an attempt to standardize the approach involving build, package, deploy, test and host.
Also, with wide adoption of JavaScript and Node.js, PaaS providers could not sit back and relax. Well known providers such as Amazon WS, Google App Engine, Heroku, Microsoft Azure, and others provide tools and capabilities for deployment of Node.js based applications.
Conclusion
Node.js as a platform has already seen wide adaption in the recent past and evolving at an enterprise-level besides few genuine concerns around enterprise readiness of it. Many of us have seen the traction in cloud adaptions at an enterprise level despite the fact that there were few concerns surrounding security, portability, adaptability, etc. At the same time Cloud Service providers were working on probable solutions and service offerings by bringing in capabilities that met enterprise needs. In the same line, with many advantages being seen by using Node.js, I expect adaption by Organizations to only increase in the near future. In fact there are few who are already using it in their enterprise and consumer facing applications:
Google using Node.js in few of their projects but they are not open about it
LinkedIn is using Node.js for their Mobile based RESTful API
PayPal now has almost all of their web applications using Node.js platform
Yammer platform heavily uses Node.js platform
GoDaddy uses Node.js to develop all of their consumer facing applications
Many more…
Accept the fact that the JavaScript is already a popular programming language (as per GitHub community), so is Node.js, AngularJS, MongoDB and other JS based frameworks. Organizations and developer community is moving towards rapid development so that they can react quickly due to ever changing business needs and service offerings.
However, emergence of JavaScript and Node.js at server-side is by no means death knell to popular programming languages such as Java, .Net, PHP, Ruby, and others. These languages have their own prominence and they will be here to stay for long run and it does have its relevance for many years to come.