Node.js for beginners

May 11, 2021Code , Design , Development , Tech , Technology

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 on your local environment.

So for this first part, we are going to learn about:

  1. How to install Node Js.
  2. My first node js application
  3. 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.

  1. Once downloaded, proceed to install it like any other common installer.
    1. Open the installer 

  1. Follow the steps, click on the Next button, accept the terms and conditions.

  1. Set the path installation, I recommend using the default path for installation.

  1. Use the default configuration for the components

  1. Finally, for the last step click on the Install button 

  1. 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.

  1. 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

  1. Type the following code on the firstApp.js file:
    1. /* First App, comment example */
    2. console.log(“Hello, World!”)
    3. // Declare variables
    4. var vString = “Hi”;
    5. var vInt = 100;
    6. var vBoolean = false;
    7. var vArray = [‘1′,’2′,’3’];
    8. var vObject = {
    9.     name: “Jorge”,
    10.     age: 30,
    11.     active: true
    12. }
    13. console.log(vString + ” “+ vObject.name);

  1. Click on the View menu and select Terminal 

  1. We can see the terminal section open in the workspace folder 

  1. 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 on your code different types of variables, using numbers, text, arrays, 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.

  1. 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.

  1. 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).

  1. Now we need to run our server, to proceed with it we need to type in the terminal section the command “node server.js”

  1. 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…


Related post

Implementing a Live Streaming feature on your site with C# .Net Core and AWS

Code , Development , Technology

These days, Live Streaming services are causing a great impact in terms of staying connected with other people, now during this pandemic, the technologies we have are capable of serving these needs. In this post, I want to describe a few points and recommendations about which technologies we can use to embed your existing live-streaming …

Read more
What to look for when hiring a software developer from an outsourcing company?

Development , Outsourcing , Technology

There are some key factors to consider when hiring a software developer from an outsourcing company. Outsourcing has become a common practice, especially when it comes to software development since it provides a cost-effective solution when hiring process developing talent. Although you will avoid the usual pains of the hiring process, it’s always good to …

Read more

Leave A Comment

Your email is safe with us.