This blog is focused on Node.js. So, it only seems logical to start things off by trying to explain what NodeJS is. I will try my best to break down what this platform is all about and what it allows us to do.

You may have heard about NodeJS as: “Javascript on the server”. But can we dig deeper than that? Sure we can!

The Node.Js website (https://nodejs.org/eng) welcomes you with this paragraph:

“Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.”

Alright, that’s a mouthful, let’s try to break this down!

First, “is a JavaScript runtime”. Yep, Node.Js is written in Javascript. The most popular language in the world  also runs this platform. This means developers can use the same language on the front-end AND the back-end.

Next, “uses an event-driven, non-blocking I/O model”.  What??

Node uses a event loop. This event loop handles dispatching events in your program. It handles requests and responses, and if there is none, the event loop is sleeping, just waiting for something to happen.

The non-blocking I/O (input/output) means that your program keeps running while the server processes a request.

So, if Node receives several requests, the server is able to handle them asynchronously. They all get treated as soon as they are received. The server doesn’t wait for a previous request to be completed to start a new one.
Conventional I/O model will use a blocking model, meaning the execution of the program stops while the request is being processed. There are ways to process several requests at the same time, but they are more demanding in resources and can cause latency.
Example: Reading data from a file with Node.Js

 const fs = require('fs');

//event loop receives this and is handled outside of the loop
 fs.readFile('/myfile.json', function(data){
 console.log(data);
 });
 //program keeps running
 //when the response comes back, the callback is executed

 

Next, we have ‘lightweight and efficient’.

It’s pretty self-explanatory. I suggest you visit NodeJS GitHub’s page (https://github.com/nodejs/node) and realize how small it is. And this is what I love about it. The freedom to do whatever you want with Node. Node doesn’t have an opinion, Node is just a platform, Node is not a framework. It gives you a foundation to build on. And that foundation is solid, but minimalist. So there are a lot of possibilities and this is what makes Node fast.

You may ask: “That’s great but, if Node is minimalist, how can there but so many possibilities?”

You add what we call packages with a tool called NPM. NPM is the Javascript’s package manager. Basically, the Javascript community build packages and they are accessible by everyone.

“Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.”

Just to give you an idea, there are more than 330 000 modules in the NPM library. I’m sure you can imagine the level of commitment from the Javascript community in this tool. Let alone all the ideas and possibilities that all those modules add to NodeJS.

Let me add a few more advantages about NodeJS:

  1. – JSON(JavaScript Object Notation) is a popular way to format data, and is native to Javascript.
  2. – Javascript is a language used in some NoSQL databases (MongoDB, CouchDB)
  3. – NodeJS use Google Chrome’s V8 Engine, which stays up-to-date with the Javascript language standards. Meaning, you don’t have to wait for your browsers to catch up with the javascript you are writing.

I hope you have a better understanding of what Node.Js is. The next logical question would be: What can I actually do with this?

  1. NodeJS is designed to handle real-time applications, what we call DIRT( Data-Intensive Real-Time). You can think of a chat room for example, or a multiplayer game. Socket.io is a popular tool is this regard.
  2. NodeJS is also perfectly able to handle Web Applications. You may have heard of the Express framework.
  3. Command Line Applications
  4. APIs (Application Programming Interface)

I’m going to leave you with a snippet of code that create a http server with Node. You’ll see how little code you actually need.

const http = require('http');
 http.createServer(function(request, response){
 response.writeHead(200, { 'Content-Type' : 'text/plain'});
 response.end('Hello World\n');
 }).listen(3000);
 console.log('Magic happening at http://localhost:3000');

 

First, we require the http module, which Node provides out of the box.
Then, we create a server. The callback is run every time the server receives a request. It takes two arguments, request and response.
We define the type of data that our response will send (here just text/plain). We then end the response sending ‘Hello World’. Finally, we tell our server to listen to port 3000.
To start the server, go to your terminal, and type node <fileName>. You will see “Hello World” at http://localhost:3000 and the console.log message in your terminal.

That’s it!
I hope I have been clear enough in my explanations. Feel free to ask questions or correct me if I made some mistakes.
Feel free to share!

Sources:

Read more