[Node.js 101] 1. Getting started

In a simple answer, the makers of Node.js took JavaScript, which is normally confined into a browser and they allowed it to run on your computer.

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.

Getting started with a simple ‘Hello World!’ as a result, after the Node.js installation, we create a index.js file in any preferred folder and put the following line of code into it using any text or source code editor:

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

Node.js allow us to create a simple HTTP server that can show us the ‘Hello World!’ message in a browser. Using the same instructions above, now we create a new file, server.js, with the following code:

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

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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store