top of page
  • Writer's pictureAnushka Shrivastava

Hello World in Node.js

Updated: Oct 5, 2022

Node.js is an open source server environment that runs on various platforms such as Windows, Linux, Mac OS, etc. Node.js uses JavaScript as its programming language. JavaScript was initially developed for front end developers. But with growing age, it is now used for server-side coding as well. This is possible due to the presence of Node.js.

Node.js is a very popular framework and tool which can make any kind of project. Let us start coding in Node.js with our very first program which is important in path to learning of any programming language or framework – Hello World.

Let the name of the program be helloWorld.js

First, we will be looking at the code and the output produced by it. And then we will understand the meaning and working of each statement.

Our hello-world code looks like –

const http =require('http');

const hostname ='localhost';
const port =3000;

const server = http.createServer((req, res)=>{   
    res.end('Hello World\n');
server.listen(port, hostname,()=>{   
    console.log(`Server running at http://${hostname}:${port}/`);

Now, type “node helloWorld.js” on terminal to connect to the server.

node helloWorld.js

The output produced in the terminal after running above command will be –

On clicking on the link http://localhost:3000/ , as given as output in the terminal, our web browser opens, and following output is produced –

We now try to understand the working and meaning of program.

In the above code,

  • The code first includes the Node.js http module. It is a built-in module of Node.js, which is required to transfer data over the HTTP (Hyper Text Transfer Protocol) for networking.

const http = require('http');

  • Hostname and port number are then defined for the server.

const hostname = 'localhost';
const port = 3000;

  • The createServer() method of http module, creates a new server and returns it to the constant variable server.

  const server = http.createServer((req, res) => {
    res.end('Hello World\n');

  • The server is set to 'listen' on the specified hostname and port. When the server is ready, the callback function is called. In this case, it is informing us that the server is running on specified hostname and port.

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);

Here, ()=>{.....} inside server.listen() method is defined as a callback function. It is a concept of JavaScript.

  • The “req, res” passed as arguments while creating server are two important objects to handle the HTTP call. The first provides the request details. It is not used this code example. The second one is used to return data to the caller. We close the response , adding the content as an argument to end().

res.end('Hello World\n');

We have now successfully made and run our first program in Node.js. Hope you enjoyed the post and got comfortable with coding in Node.js as beginner.


20 views0 comments

Recent Posts

See All
Graphic Cubes

Subscribe To Get Latest Updates

Subscribe to our newsletter • Don’t miss out!

Thanks for subscribing!

bottom of page