Hi everyone, today we are going to start a little tutorial on Node.js for beginners. This is a Javascript framework commonly used to create microservices. I’ll try to go through each step to make it easier to follow along, I will also provide the code on Git to help you when trying this in your local environment.
So for this first part, we are going to learn about:
How to install Node Js.
My first node js application
My first node js web server
We need:
A good and stable internet connection
Visual Studio or other programming tools for coding
How to install Node Js.
1. To start you will need to download the installer for node js.exe on your computer from this URL. I recommend downloading the LTS version because it’s more stable. As a reference, I’m currently using Windows 10 as my operating system.
2. Once downloaded, proceed to install it like any other common installer.
a. Open the installer
b. Follow the steps, click on the Next button, and accept the terms and conditions.
c. Set the path installation, I recommend using the default path for installation.
d. Use the default configuration for the components
e. Finally, for the last step click on the Install button
f. To validate the node js installation, we need to open the terminal or CMD command line on our computer. After that type the command “node – v” and we can see the node js version installed on the computer.
Now you’re all set to start using node js on your computer.
My first node js application
To start, you will need to download Visual Studio Code (you can download it here). I recommend creating a folder on your hard disk with the name “Workspace” and a subfolder “node” for your code. It should look like this:
Let’s start.
1. Create a new folder called firstApp, open this folder on visual studio code and create the file firstApp.js
2. Type the following code on the firstApp.js file:
/* First App, comment example */
console.log(“Hello, World!”)
// Declare variables
var vString = “Hi”;
var vInt = 100;
var vBoolean = false;
var vArray = [‘1′,’2′,’3’];
var vObject = {
name: “Jorge”,
age: 30,
active: true
}
console.log(vString + ” “+ vObject.name);
3. Click on the View menu and select Terminal
4. We can see the terminal section open in the workspace folder
5. Now, type in the terminal “node firstApp.js” this is a node command to indicate that we need to run our firstApp.js script, you will see in the terminal the result of the code we typed.
That’s it for now, I recommend trying to use your code with different types of variables, using numbers, text, arrays, and JSON objects to get familiar with node js. You could also practice with operations, bear in mind that this uses javascript so you can create javascript functions to code it.
My first node js Server application
1. Using the same folder from the previous example, we must create a new file called “server.js” using visual code.
2. We need to import the HTTP module from Node.js, therefore we should type the following code in our server.js file:
var http = require(“http”);
Note that the “require” function is used to import node js libraries or modules.
3. Now we will use the “http” var to create a web server, this is how the code looks like:
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {‘Content-Type’: ‘text/plain’});
// Send the response body as “Hello World”
response.end(‘Hello World\n’);
}).listen(8081);
In the first line, we are using the createServer method that expects two parameters, the request, and response. The request is all the params that the user sends when accessing the server, and the response is all messages that we will respond to the user.
The response.writeHead will indicate that we are responding to a simple text.
The response.end we are specifying that we will return the sentence “Hello world”.
The command listen establishes that we will listen to port 8081 (this can be changed to any other required port).
4. Now we need to run our server, to proceed with it we need to type in the terminal section the command “node server.js”
5. We must open our web browser and run the IP address http://127.0.0.1:8081/ to see our first Node js. Web server.
That’s it for now. So see you later…