Node.js isn't completely new, but it's quite recent, not just in term of code, but also as an idea.
Let's me explain...
Front-end vs Back-end development
In a traditional website, you have some code running in the browser itself (Front-end code, written in JavaScript and in a certain way HTML and CSS) and you have other code running on the server (in PHP, Ruby, Perl, .NET, Python, etc...).
Code running on the server is managed by the server itself, so provided you have a server able to run your language of choice, you can run whatever you'd like! You could write your own new language and provided your server supported it, it would run properly on your website.
But the front-end code runs in the browser of your clients, which are Google Chrome, Mozilla Firefox, Microsoft Edge or Internet Explorer, Safari, Opera, etc...
Not only would a new language require to be supported by each of the browsers used by your visitors, but your visitors would need to upgrade their browser which isn't always something they can do (perhaps due to corporate policy or outdated apps).
This means that a Full-Stack developer (someone who codes both in the Front-End and the Back-End, and thus, the Full-Stack), needs to use at least two programming languages:
- JavaScript for their front-end code
- Something else (often PHP) for their back-end code
Node.js is an attempt to unify both halves of the Full-Stack by letting web developers use JavaScript for server development.
My initial reaction when I heard that was: "but JavaScript isn't powerful enough to do that!!!", but I was wrong. Very wrong...
The solution was simply to extend what JavaScript could do with modules and code to fully support normal back-end operation like Database access and file management which are absent from the Front-End.
A revolution in the web server
In itself, just bringing JavaScript to the back-end isn't interesting in itself. Sure, it unifies the stack by using a single language, but it's not enough to cause the major paragism shift that Node.js is causing.
You see, between the Front-End and the Back-end sits the web server, the two most popular being Apache and Nginx (pronounced "Engine X").
When an Apache website is visited, multiple request are usually made: one of the document, another for each of the CSS files and JS files that are loaded, additional ones for the images and finally, calls for each of the AJAX requests (behind the scenes calls made by JavaScript in the Front-End to request or send (or both) data to and from the back-end in the background.
If you've seen your FaceBook feed update when someone types a comment, that's AJAX.
Ajax is an acronym: Asynchonous JavaScript And XML. The key word to keep in mind is "Asynchronous".
The problem is that when the back-end is hosted by Apache, the AJAX requests, which are Asynchronous in nature, are handled by a web server which is synchronous in nature: it blocks until a call is resolved.
Node.js solves that by being completely asynchronous (and non-blocking) by nature. That's what Node.js bring to the table: a revolution in how web requests are made.
In a nutsell That's the same difference between calling and texting a friend. When you call a friend, the only way to convey information back and forth with him is if you are both on the line at the same time: your ability to send him a message is blocked by his inability to answer you at that very moment.
With texting, you can send him a message, forget about it, and eventually, get a response. You are not glued to your phone to deliver him a message.
Nginx vs Node.js
I mentionned NginX above, and like Node.js, it is non-blocking but Nginx performs the asynchronous management at the server level, while Node.js does it at the application level, and that's rather fondamental.
When you make a request to an Nginx server, your process still waits for NginX to answer, but Nginx is able to reassing the worker you used until a response is ready. In otherword, if you request a report which takes 60 seconds to run, you will wait for the 60 seconds, but Nginx will not block one of it's workers for that process, it will be able to handle more other request while you wait.
Node.js on the other hand, doesn't make you wait. It accepts your request, processes it offline while you are not waiting for it, and will call you back once it's ready.
In order of ascendance, if you ask a report which takes 60 seconds:
- Apache will make you wait on the phone, and waits on the line with you until your report is ready. Too bad if other callers are in the call queue or if you have other calls to make
- Nginx will make you wait on the phone, but will put you on hold to handle other calls until your ready is ready. Other callers are served, but do bad if you have other calls to make
- Node.js will take your message, hang up, and call you back once your report is ready. You both get to make and received other calls
So why I am not a Node.js developer yet??
My company handled a few Node.js contract when one of our employees, Luke, was hired to handle our Node.js contracts. The problem we faced, was that at that moment, we didn't have any servers with the capacity to host Node.js applications and most of our contracts were in PHP/JavaScript. We still delivered (Thank you Luke), several Node.js projects and I got to review his clear and beautiful code which make me want to trade job with him!
But when Luke left to found his own Node.js firm, we lost the internal knowledge to keep going but I kept dreaming of the day I would also code in Node.js.
Will Node.js take over PHP then?
And bring us to the next problem with Node.js: it's not as mature yet as PHP is. Node.js is improving with more and more Frameworks and tools, but until then it remains mainly for specialized high traffic or high capacity applications and isn't yet in the main development environment.
I believe that long term however, Node.js can be a force to reckon with and will allow to build rich very fast applications that react almost instantly.
Until then, many benchmarks that shown that overall, many PHP Frameworks (including Laraval), are able to match or beat Node.js for regular traffic volumes (even if Node.js is often able to handle higher traffic volumes).