Node.js – Getting started on Ubuntu 14.04 (Trusty Tahr)

In this article we are going to have look into the installation and basic usage of node.js application. Node is a set of libraries for JavaScript which allows it to be used outside of the browser. It is primarily focused on creating simple, easy to build network clients and servers.

Insatallation.

You will have to download the package from the official site here. After the download type the following commands in the terminal.

The first thing you’ll notice is that this program, unlike our first one, doesn’t exit right away. That’s because a node program will always run until it’s certain that no further events are possible. In this case the open http server is the source of events that will keep things going. Testing the server is as simple as opening a new browser tab, and navigating to the following url: http://localhost:8080/. As expected, you should see a response that reads: ‘Hello Http’.

You can also check that by opening a new terminal window and typing:

Now let’s have a closer look at the steps involved in our little program. In the first line, we include the http core module and assign it to a variable called http. You will find more information on this in the next section about the module system. Next we create a variable called server by calling http.createServer. The argument passed into this call is a closure that is called whenever an http request comes in. Finally we call server.listen(8080) to tell node.js the port on which we want our server to run. \ If you want to run on port 80, your program needs to be executed as root. Now when you point your browser to ‘localhost:8080’, the connection closure is invoked with a req and res object. The req is a readable stream that emits ‘data’ events for each incoming piece of data (like a form submission or file upload). The res object is a writable stream that is used to send data back to the client. In our case we are simply sending a 200 OK header, as well as the body ‘Hello Http’. So here are the basics of how to use node.js anny quuestions comment below.