Creating Node.js First Application

In this tutorial let us look into how to create the application in Node.js. Basically, there are 3 simple steps to create this simple node.js application.

  1. Import Required Modules – We will be using require directive to load the HTTP node.js module. We will discuss more on modules in details later.
  2. Create Servers – We need to create a server which listens to client’s request. It’s similar to IIS or Apache HTTP server.
  3. Read the request and return response – The server created in the above step will read the HTTP request made by the client which can be a browser or console and returns the response.

Creating Node.js First Application

Step 1: Import the required module

In this example, we are building a simple web application which returns “Hello World” . We can use require directive to load the HTTP module and store it into a variable called http.

Step 2: Create the Server

Now we have loaded the HTTP module we need to create the server. This can be done by creating the instance and calling http.createServer() method and bind it to the port 8080 using the listen method. We need to pass the function arguments with request and response. To create the server follow the below steps.

  • Open the command prompt and type node to start the node.js
  • Copy the below code and paste in the Node REPL terminal and hit enter

Step 3: Open the browser and type the URL and you should be able to see the string “Hello World” in the browser as shown in the below screenshot.

Creating Node.js First Application

Creating Node.js First Application


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.