Node.js is a server-side javascript interpreter. Since javascript is already used for client-side scripting,
switching contexts between client and server is easy for developers.
Ofcourse, there is more to node.js than just the server-side javascript label. Another
major attraction of node.js is that it allows event driven programming, making it
suitable for building high-performance, scalable network systems
Non-blocking I/O and event driven programming
One of the major disadvantages to thread-based programming is that I/O
operations block the calling thread. This means, any time there is an Input/Output
operation such as writing to disk, reading from disk, waiting for user input, querying
the database, and such, the calling thread waits and will not do anything in the
meantime.
To provide a simple analogy, assume you have the following tasks to do:
1. Write a letter to grandma who has no phone or Internet access
2. Bake Chicken
3. Sort your bills
You begin with writing a letter to grandma. But half way through, you realize you
are out of ink. So you send somebody out to buy you ink and in the meantime, you
wait. When the ink arrives, you finish your letter and then begin baking the chicken.
But you again wait because baking takes time. When the chicken is done, you start
sorting out your bills. See how inefficient the whole model is? This is what happens
when you are building a thread-based system. Ofcourse, you could speed up the process
if you can get help from two other people to bake the chicken and sort the bills
simultaneously. But now you are spending more resources.
This is where event-driven programming comes in. In this model, you would begin
writing the letter. When you run out of ink, you send somebody to buy ink. Meanwhile,
you don't sit around biting your nails. Instead, you put the chicken in the oven
and set the timer. Again, you don't wait. You begin sorting the bills.
If you are interrupted by either the ink arriving or the oven timer ringing, then
you attend to those specific events by continuing the unfinished letter or taking
out the chicken from the oven respectively. Otherwise, you continue to sort the
bills until that task is completed.
Clearly, the event-based model wins in terms of speed and resource requirements.
So if you are building servers that lean heavily on I/O operations, node.js, with
its event-driven asynchronous programming capabilities is particularly endearing.
Node.js allows you to do event-driven asynchronous programming easily, which makes
it particularly endearing for building high-performance scalable network systems!
Until recently, traditional servers including webservers like apache and
IIS have been following a thread-based model. To achieve scalability, network programmers
had to rely on multi-threading, making both development and debugging complicated.
But with the advent of node.js, both these tasks have been vastly simplified, paving
the way for building scalable networks with minimal code and maximum stability.
So if you are building servers that lean heavily on I/O operations, node.js is the
way to go.