[Node.js 101] 1. Getting started

[ What is Node.js? ]

Node.js is a powerful JavaScript based platform built on Google Chrome’s JavaScript V8 Engine that runs on your machine allowing actions that was not possible normally with JavaScript as, access your computer’s files, can listen to network traffic, HTTP requests that your machine gets and send back a file, access databases directly, etc…

You can visit their official website (https://nodejs.org/) to know more about it and download the Node.js Installer where is available for Microsoft Windows, Linux, macOS, SmartOS, FreeBSD, OpenBSD, IBM AIX and z/OS. In this article we have installed Node.js in a Microsoft Windows 7 machine.

[ ‘Hello World!’ in a Command Prompt ]

console.log("Hello World!")

After saving it, in a Windows 7 machine we can SHIFT+RIGHT MOUSE click on the respective folder and select “Open Command Window here” item of the context menu to open a Command Prompt Window with the path where the index.js file exists.

(For Windows 10 users if not active, that feature can be activated with some additional steps, where in the future will be a article explaining how to do it but for now you can run the command prompt normally and navigating forward using ‘cd [foldername]’ or backwards using ‘cd..’ until you reach the folder where the index.js exists.)

On the command prompt window, execute the following command to obtain the desired result:

node index.js

[ ‘Hello World!’ in a HTTP Server ]

var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('\nHello World!\n');
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');

On the command prompt window, execute the following command to obtain the result:

node server.js

The result of the command execution will show us a message (that was been coded) warning us that the server is running. If we open a browser and typing the respective link, we will obtain the following result:

To shutdown the server, on the command prompt window, press CTRL+C where the server will no more be available.

Hope you liked this small article, best regards, Ricardo Costa (Richards).

This article was created in a context of the Distributed Systems Class 2020–21, ESTG-IPG.