Writing Your First Node JS Application

Writing Your First Node JS Application

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

The most common example Hello World of Node.js is a web server:

Before creating an actual "Hello, World!" application using Node.js, let us see the components of a Node.js application. A Node.js application consists of the following three important components -

  • Import required modules - We use the require directive to load Node.js modules.
  • Create server - A server which will listen to client's requests similar to Apache HTTP Server.
  • Read request and return response - The server created in an earlier step will read the HTTP request made by the client which can be a browser or a console and return the response.

Creating Node.js Application

Create an app.js file and add the below code in it:

Step 1 - Import Required Module

We use the require directive to load the httpmodule and store the returned HTTP instance into an http variable as follows -

const http = require("http");

Step 2 - Define Port

We define our port application port on 3000. It basically means our app will be available on port 3000 and if we visit http://localhost:3000 we can access our app.

const port = 3000;

Step 3 - Create Server

const server = http.createServer((req, res) => {

 res.statusCode = 200;

 res.setHeader("Content-Type", "text/plain");

 res.end("Hello World!\n");

});

The createServer() method of http creates a new HTTP server and returns it. The server is set to listen on the specified port. When the server is ready, the callback function is called, in this case informing us that the server is running.

Whenever a new request is received, the request event is called, providing two objects: a request (an http.IncomingMessage object) and a response (an http.ServerResponse object).

Those 2 objects are essential to handle the `HTTP call.

The first provides the request details. In this simple example, this is not used, but you could access the request headers and request data.

The second is used to return data to the caller.

In this case with:

res.statusCode = 200

we set the statusCode property to 200, to indicate a successful response.

Now, we set the Content-Type header:

res.setHeader('Content-Type', 'text/plain')

and we close the response, adding the content as an argument to end():

res.end('Hello World\n')

Step 4: Listen

server.listen(port, () => {

 console.log(`Server running on http://localhost:${port}/`);

});

listen()helps the server to listens for incoming traffic at port 3000 i.e. it waits for a request over 3000 port on the local machine and if any request arrives the server will handle/respond to that request.

Whole code in app.js will look like this:

// app.js

// import http module
const http = require("http");

// define port
const port = 3000;

// create server
const server = http.createServer((req, res) => {

 res.statusCode = 200;

 res.setHeader("Content-Type", "text/plain");

 res.end("Hello World!\n");

});

// listen on defined PORT
server.listen(port, () => {

 console.log(`Server running on http://localhost:${port}/`);

});

Now execute the app.js to start the server as follows -

node app.js

Open http://localhost:3000/ in any browser and observe the following result.

Live Code Example:

 
Share this