Node.js is part of MEAN - the new technology stack used in web development, along with MongoDB, ExpressJS and AngularJS.
However, Node.js is not just for web development. The definition supplied by the official documentation states that“ Node.js is a JavaScript runtime built on Chrome V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.”. So, you can build any type of application, but its programming model makes it perfect for data-intensive real-time applications that runs across distributed devices.

In essence, Node.js is a platform on which you can run programs written in JavaScript. For a web developer this sounds a bit strange: wasn’t JavaScript built for web, to be executed within a browser? What it is good for if there is no HTML to manipulate?. In fact, JavaScript is just a programming language like any other. It does not have native capabilities for DOM manipulation, making Ajax requests or working with the implicit browser objects like window, document or navigator. All these are capabilities added by the browser on top of JavaScript. Outside of a browser, JavaScript looses these capabilities.

Why using JavaScript and not other language? First of all, it is very fast; it uses V8 JavaScript engine which also runs inside Chrome browser. Secondly, it is very popular. Any web developer who developed a dynamic website, if it was in Java, PHP or .NET, at some point had to write some JavaScript. Any web designer, if he/she wanted to add usability features to the website, had to use or write some JavaScript code. It is the common denominator of all web developers, no matter the server-side technology so why not push it to the server-side also? And finally it is very simple and very easy to learn.

Node.js comes with npm (Node Packaged Module), a package ecosystem which allows developers to import and reuse JavaScript functions written by others or to publish their own functions for the public use (if you want something similar, think of Maven repositories for JAVA). One might say why not just import the file using <script> tag (assuming we are looking at a web application). Two things makes the difference:

•     any JavaScript includes using <script> tag is loaded in the global namespace (so watch out for conflicting variables or function names, etc.). Using modules, you can sandbox the imported functionality in a locally namespaced variable;

•     using modules, you can explicitly declare dependencies (like for example, to use a jquery plugin you need to have jquery library). By declaring the dependencies, all necessary modules will be imported (if module A depends on B, and B depends on C then the npm will get you also module C even if is not a explicit dependency that you specify). With <script> tag you cannot do that. You just have to previously import all dependencies before importing the desired functionality. 

If we look at the Node.js documentation, the “Hello World” example, is actually a very simple web server.

________________________________________________________________________________________________________________________________________

const http =require('http');

http.createServer( (request, response) => {

        response.writeHead(200, {'Content-Type': 'text/plain'});

        response.end('Hello World\n');

}).listen(8124);

________________________________________________________________________________________________________________________________________

To run it, just put the code in a file, helloWorld.js and execute it with node program:

node helloWorld.js

If you access the URL http://127.0.0.1:8124/ you will get an HTML page with  the text Hello World on it.

Let’s analyze a bit this example so we can understand how Node.js is working:

  • •      first you declare that you need a module called “http”, by using require; this is the name of the module in NPM library, and Node.js will know it needs to import it before running the code;  “http” is part of the standard Node.js installation. If you want to use a module that is not part of the standard installation you need to download it from the module repository using npm command;
  • •      the module contains a function “createServer” which creates a HTTP server that will listen on port 8124 and will respond with HTTP 200 OK and text “Hello World” on any requests received on that port on local machine.

Every npm module has a descriptor file, called package.json. As the extension suggests, is a text file containing a JSON object with all metadata about the module like:

  • •     name, url, homepage, etc; 
  • •     available versions - if you want to install a version that is not in the list, the installation fails;
  • •     dependencies  - a list of runtime dependencies of the module (other modules on which the functionalities of the modules depends upon)

To install a npm package/module you type:

npm install <package_name> —save

and it will bring you the latest version available, and also updates your package.json file with the dependency to the package you just installed.

If you want to install a package globally (to be available on Node platform for other applications) you do:

npm install -g <package_name>

Very simple and easy to use. This is all there is to know in order to start working with Node.js

Node.js is available on every major operating system platform: Windows, Linux, MAC OS X. Being a platform independent on the operating system, you can develop on your Mac laptop and deploy the solution on a Linux server, without any modifications to the source code.