Node JS Introduction

About Node.js

Node.js is runtime environment for JavaScript. That means you can write JavaScript code wherever Node.js is installed. In other words, it is JavaScript on the server-side but not only.

Node.js is intended to be used for building web applications that have to handle much web traffic. That’s because multiple requests client-server are run on a single threat which means that it provides more scalability and performance for any web application written with Node.js. For example, Node.js is suitable for building real-time web applications such as chat rooms, collaboration tools, online games, etc.

Node.js is event-driven and asynchronous

Synchronous vs asynchronous

Synchronous model – Let’s say that you have one hundred lines of code that have to be executed. In synchronous programming these lines of code are executed one by one in the order they appear in the document. If one line of code, let’s say the 45th, is time consuming, then the lines of code that follows have to wait until the 45th line is completed.

Asynchronous model – The asynchronous model follows a different approach. The lines of code that follow the 45th line of code are divided into two parts. One part contains the code that is dependent of the 45th line of code and the second part that contains the code that is independent of the execution of the 45th line.
So, we wrap the dependent code in a function with the result of the operation as its parameter, and register it as a callback to the operation on its success. Once the operation completes, the callback function will be triggered with its result.

Example:


console.log('One');
console.log('Two');
setTimeout(function() {
	console.log('Three');
	}, 2000);
console.log('Four');
console.log('Five');

A synchronous language has a two seconds delay before writing “Three” on the screen and then continue to write “Four” and “Five”. An asynchronous programing language writes “Four” and “Five” before writing “Three”.

setTimeout() is a function. In our example it takes in two parameters. First parameter is an anonymous function that writes “Three” on the screen. The second parameter is 2000 (which means 2000 milliseconds). The function that actually logs “Three” is known as a callback to the setTimeout function.

npm – the Node Package Manager

Let’s be honest, one of the reasons for the big success of Node.js is npm, the biggest package manager out there. There are over 150,000 packages ready to be downloaded and used in your application. To install one package all you have to do is to type npm install package command in terminal. You can learn more about npm at http://npmjs.org website.

Node.js is not only a server, it is much more

Node.js is used by many to write JavaScript on the server-side. But Node.js is much more than that.
It can be a powerful solution to create command-line tools as well as fully-featured, locally run applications that have nothing to do with the Web or a browser. One such example would be https://gruntjs.com/.
Are you into robotics? Node.js got you covered! Cylon.js is one of the JavaScript frameworks for robotics. You can learn more about this framework at https://cylonjs.com/.
If you use Atom editor (which is a great tool) then you might want to know that it was developed on Node.js.

Real-time web applications with Node.js

Node.js has at least a couple of popular frameworks that can be used to build real-time web applications. They are Socket.io and sock.js. If you want to build a tool like Google Drive for example, then these two frameworks make it very easy.

Networking and I/O with Node.js

Core modules of Node.js contain very powerful networking and file system tools that allow you to create server and client applications that accept network connections and communicate via streams and pipes.

What is io.js

The io.js is nothing but a fork of Node.js created to be always up to date with the latest development on V8, the engine Node.js is built on, and the developments in the JavaScript community. Basically, Node.js is concerned more with stability while io.js with the latest development. It’s a good idea to keep an eye on both if you want to be up-to-date with the Node.js world.

Node.js sample server

Below you can take a look at a simple Node.js server. Of course you need the initial Node.js development set up in order to see the result.


var http = require('http');
http.createServer(function (req, res) {
	res.writeHead(200, {'Content-Type': 'text/plain'});
	res.end('Hello World\n');
	}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080');

Leave a Reply