Introduction to Node.js

By kedinn 2014/09/06

Node.js is programming surroundings that run in the servant based on Javascript.

As we know, Javascript is a programming language executed in the servant by different virtual machines.

Node.js uses the virtual machine of Google Chrome: V8 for its execution; that is to say, it has the same functionalities of Javascript that it owns in the navigator of Google.


The first a to consider when we worked in Node.js is that it is surroundings that allow us to realise real-time applications. 

This does not allow to Javascript to the being an asynchronous language it, but we have a single synchronous process in our code, all our application will be synchronous.

Node.js is able to do everything what we wish, but in some points very well did not take it, always we must focus in which its main function is to realise real-time applications. NPM (Node Package Modules) is the manager of packages in Node.js par excellence. It is of easy use, thought for programmers.

In NPM we have diverse commandos, the main one is “install? with which we can install packages of global or local way.

Between which we can do is: 

- To update our file package.json and/or to install the dependencies that contain this.
- To look for packages
- In which already it comes including in the installation of the last versions of Node.js.

Its structure is:

npm <comando>

For but information it consults the page of npm:


That is the file package.json?

Package.json will be the file that identified our project in this we will keep the name from the application, the version and the dependencies of the following form.

\ {“yam?: “name of app?, “version?: “0.0.1?, “dependencies?: “express?: “*?}

In dependencies you will place the name of the dependency and the version of the same so that soon when passes your project to another collaborator.

He will only have to execute in terminal:

npm install

And all the dependencies will settle automágicamente.

Now whenever you install a new dependency you must execute the following commando so that this file is updated:

npm install &lt; paquete&gt; --it knows

And when you wish to install a package of global way, you will only have to do:

npm install - g &lt; paquete&gt;

niciando with node.js

In order to initiate with node.js depues of to have created ours package.json we must create the file Javascript that hara the paper of the servant, in this case we will call it server.js, but the name does not matter.

In this file we concerned all the dependencies to use and we will place all the logic that will go of the side of the servant, an example of this can be:

bar express = require (‘express’);
bar server = express (); server.get (‘/’, function (req, head of cattle) \ {res.send (‘Hello World’); }); they server.listen (3000); console.log (‘Servant running in');

With this servant we are saying to him to node that when receiving request in the URL root of the project, gives back hello a world; but we see of detailed way so that it serves each part as our code.

Require () → With require (), him tenth to our server that Node bookstores we are going to load; bookstores that previously already are in the dependencies of our project.
In our servant we must define what to do with the requests that arrive from the users (GET - POST), in this case we used the aid of “Express.js?; if it is not defined what to do with requests we would have a server having run that does not do anything.
.get () → Him tenth to the server that to do when it receives a request GET in root of our application, by means of functions that always are going to receive as parameters a request of the client (req) and an answer of the servant (head of cattle).

res.send () → Him tenth that does response with the chain “hello world?.

they server.listen (xxxx) →Le tenth to the servant the port in which must listen to requests.