JavaScript has certain characteristics which makes it unique from the other dynamic languages. It has no concept of thread, but its model of concurrency is completely basing on events; i.e. it's an event driven programming language.
The same thing makes node server different than other web servers. This article describes what Node.js is, and how it is different and can be one of the most popular and powerful servers in future.
What is Node.js
In layman's word, node is just another server like Apache, IIS, TOM etc. But unlike those servers Node doesn't deal with PHP, .NET or JAVA. It executes JavaScript in the server side. Yes, JavaScript without a browser; that's Node. Node itself is not made in JavaScript completely; rather its wrappers are made in C. It just executes JavaScript. Below is a list, what Node is not and what Node is.
What node is NOT
- Node is not a framework, it's a server.
- Node wrappers over JavaScript V8 Runtime, are not made in JavaScript, but made in C.
- It's not multi-threaded. It runs in a single thread with callback concept.
- It's not for JavaScript beginners as it's very low level.
What Node is
- Node is a server which can execute JavaScript. Sort of a server side browser.
- Node is a open source, cross platform to make real time network applications.
- It provides you asynchronous, event driven I/O APIs.
- It runs single threaded event based loop, so all executions become non-blocking.
Pros and Cons of Node.js
Pros |
Cons |
1. Asynchronous event driven IO helps concurrent request handling. |
1. Node.js doesn't provide scalability. One CPU is not going to be enough; the platform provides no ability to scale out to take advantage of the multiple cores commonly present in today's server-class hardware. |
2. Uses JavaScript, which is easy to learn. |
2. Dealing with relational database is a pain if you are using Node. |
3. Share the same piece of code with both server and client side. |
3. Every time using a callback end up with tons of nested callbacks. |
4. npm, the Node packaged modules has already become huge, and still growing. |
4. Without diving in depth of JavaScript if someone starts Node, he may face conceptual problem. |
5. Active and vibrant community, with lots of code shared via github, etc. |
5. Node.js is not suited for CPU-intensive tasks. It is suited for I/O stuff only (like web servers). |
6. You can stream big files. |
|
Why to use Node
Non-blocking code
This is the strongest reason to select node as your server. Node is completely event driven and majority of the code runs basing on callbacks. This approach helps the application not to pause or sleep, but to become available for other requests. Let's explain this with an example.
Suppose the program will read a .txt file and print the content as output.
In php
[code language="php"]
[/code]
In Node
[code language="javascript"]
var fs = require('fs');
console.log("started \n");
fs.readFile("myFile.txt",function(error,data){
console.log("myFile.txt says:" + data);
});
console.log("finished");
[/code]
If you notice the code, you will find in php, in line no#3, it will start reading the file and before finishing up reading the file it can't do anything else. That means php blocks the execution there. Whereas in case of Node, in line no#3 it starts reading the file and assigns a callback to it, and proceeds to the next line. It continues executing the other parts of the program and whenever the "fs" finished reading the myFile.txt, the callback will be called to process necessary statements.
Fast processing
Node uses V8 JavaScript Runtime engine, the one which Google Chrome uses. Node has a wrapper over this JavaScript engine providing some extra facilities to build network applications. Both the Node wrapper and the V8 JavaScript engine are written using C language; which makes it really fast. It is much faster than Ruby, Python, or Perl.
Concurrent request handling
Node can handle thousands of concurrent connections with a very minimal overhead on a single process.
One environment
Using JavaScript on a web server as well as the browser reduces the impedance mismatch between the two programming environments which can communicate data structures via JSON that work the same on both sides of the equation. Duplicate form validation code can be shared between server and client, etc.
Easy to learn
Node is not something new. It's just simple JavaScript. So the JavaScript developers don't have to put much extra effort to learn Node.
Popularity and community
A lot of people already know JavaScript, even people who do not claim to be programmers. It is arguably the most popular programming language. So a popular language means a popular community. Big community means easy to get help or support.
Where Node can be the best solution
Here are few applications or areas where Node.js can be the best solution.
Web Socket Server
The non-blocking architecture of node makes it the best suited solution for socket server applications or broadcasting like applications. Chat servers can become more efficient and real time using Node.js as their base.
Fast file upload client
With node you can upload multiple files at a time. That means it is possible that a part of file1 and another part of file2 is in the server at a given point of time. This approach makes file uploading dramatically fast.
Data streaming
As node deals with callback concept, it can easily be used for streaming data flow. It can be really useful for Travel industry where they fetch results from different APIs of different suppliers.
Ad server
Well, I personally think Ad servers should be the fastest servers. Because, from a advertiser's point of view, if I can load the ads before all the contents of the page is loaded, I may catch the visitor. Another is if my ads load slower, the visitor might navigate to somewhere else. So ad servers really need to be fast and thus Node should be used.
Stock exchange software
Things has to be very much real time in case of stock updates. Node helps us to develop real time web applications.
Conclusion
Node.js came up with a new idea of event driven single threaded server programming which is achieved with callback concept. With the growing demand and popularity of JavaScript, a step to reach the server side or backend programming is really appreciable and it also raise hope to lots of JS developers. But at this moment, if we say Node is strong enough to replace .NET, PHP or JAVA it will be a hyperbole. But yes, the way JavaScript is developing, we are very hopeful that one day Node may become an ultimate solution for the web backend.