How You Should Start with NodeJs

How You Should Start with NodeJs

NodeJs is powerful JavaScript runtime which enables ability to run your friendly JavaScript on server side. If you are advanced JavaScript expert then understanding NodeJs is easy but the same will be difficult for others who are at the beginner level of learning.

This article will focus on step-by-step guide to start with NodeJs.

I assumed that you've already gone through what the hell NodeJs is and what are the different ways to install it on your machine.

Okay let's get started now!

Step 1: Consoling and Confirming Code

As a JavaScript developer, it's a good practice to test your code in a browser console before server deployment.

  1. We need a file to run code in NodeJs. So first we will create a file index.js (can be any infect, but it's good to keep meaningful name).
  2. Put below code inside index.js file.
    Snippet
    var aboutMe = {
        fullName: "Kushal Jayswal",
        expertise: "UI/UX and Frontend Technologies",
        currentExperience: "4+ years",
        myBlog: "https://teckstack.com",
        personalSite: "https://kjayz.com",
        githubTwitter: "@kutec"
    }
    
    console.log(aboutMe);
    
  3. Use node index.js code in command prompt to check whether code is correct or not.
  4. If everything goes well, it must print the file content in command prompt.
    Start With NodeJs Output
    Start With NodeJs Output

Step 2: Creating Local Server

NodeJs is an environment and not a framework. So there is nothing ready to use except API. But to use APIs we first understand each API in depth. Like all NodeJs application needs a server and we can create a local server using http module using its createServer() method.

In NodeJs, require is the most used keyword because when any internal or external modules needed, you have to include them in your file using require.

var http = require('http');

Why we required a local server?

I understand your feeling. As a frontend developer you never dealt with server. In the past you might have worked with developers who write code for servers in Java, Dotnet, PHP, etc. But with NodeJs, though you are using JavaScript, you required a server to send and receive requests from client to server and vice-versa. I know the explanation is not so technical but enough to get the idea!

Now let's move to create a local server using http module in NodeJs.

Snippet
var http = require('http');

var requesrHandler = function(req, res){
     res.writeHead(200, {
         'content-Type': 'text/plain'
     });
     res.end('Welcome');
};

var server = http.createServer(requesrHandler);

server.listen(3000, 'localhost');

Code explanation

First we have included the module http using require keyword. Then we have defined a callback function which accepts 2 arguments req (request) and res (response). writeHead(), end(), createServer() and listen() are various methods of http module.

listen() method used to define a port number for the localhost.

Run the server

As per the Step 1, you need to put the code of this section into index.js (or server.js) file and then run node index.js code in command prompt. If there isn't any error, it will give blinking cursor in the command prompt and then you have to open a browser and hit the url http://localhost:3000.

As an output, you will see whatever mentioned in res.end('Welcome');.

I hope this article will help you to get started from scratch with NodeJs. If you have any doubts or suggestions, please put your comments below and make it loud.


Also published on Medium.