Essentially it is made up of
- A thread pool to handle file I/O operations. All blocking system calls are processed here.
- An event loop library to provide non blocking capability
- Standard Node Library.
Top 3 reasons why I think, Node.js is better option than conventional client-server model for an enterprise application.
- Scaling : In traditional approach, we have an underlying Apache or NGINX or Web Sphere web server or Application Server, with Java or PHP running on top of it. Each new connection to the server spawns a new thread, and it’s very easy to quickly lose performance – the only way to support more users being to add more servers. It simply doesn’t scale well. With Node, this isn’t the case. There is no underlying web or application server to listen for incoming connections and return HTTP status codes – you’ll need to handle that core server architecture yourself in the application code. Hard? Luckily, there are modules to make this easier. You just pick and use a module.
Why Not Node.js
Node.js really shines in building fast, scalable network applications, as it’s capable of handling a huge number of simultaneous connections with high throughput, which equates to high scalability. See this example
But, We can’t rely on Node.js always. I believe, following are some cases where Node.js may not be an ideal solution
- Heavy / CPU intensive work – Node.js, by default, is a single thread model uses single core CPU. Node.js offers some level of clustering to make use of multi core CPU architecture but it is still in initial stage. Currently, people run several Node.js server instances to achieve multi tasking. With clustering, it is possible to offload all heavy computation to background processes (batch jobs) written in a more appropriate environment for that, and having them communicate via a message queue server like RabbitMQ.
- Huge File I/O work – for the same reason above.
Node uses common.js for modules – Modules are an integral piece of any robust application’s architecture and typically help in keeping the units of code for a project both cleanly separated and organized.
What this results in is a reduction in the likelihood of our function names conflicting with other functions defined in additional scripts on the page.
With modules, you can sandbox the imported functionality in a locally name spaced variable. e.g.
var logger = require(‘some logger module’);
Also, you can explicitly declare your dependencies with modules (this is very imp feature) – importing Module A transitively imports dependent Modules B and C also.
ES6 made modules are part of JS specifications now. You can define classes in JS
NPM (Node Packaged Module)
Node installation, by default, comes with NPM. NPM modules are publicly available, reusable components written by community for various regular tasks. These modules are available through easy installation via an online repository, with version and dependency management.
All these module can be found @ https://npmjs.org/ and these can be installed using command line interface that comes with node installation.
Few examples of NPM modules are:
- express – Express.js is web development framework for Node.js used by majority of Node.js applications.
- connect – Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance “plugins” known as middleware; serves as a base foundation for Express.
- socket.io and sockjs – Server-side component of the two most common websockets components out there today.
- mongo and mongojs – MongoDB wrappers to provide the API for MongoDB object databases in Node.js.
This is pretty simple. Just get an installer from here https://nodejs.org/en/download/ and follow the on screen installation instructions. Once installed, you can check the versions to make sure all is well.
Hope you enjoyed this post. You may want to try our MEAN stack experiment here.