make hello world application using node js

How to Make “Hello World” application using Node JS?

Here I will show you how to create your very first “Hello WorldNode js application.

A Hello World application is a simple program where we display the message “Hello, World” to test that the Computer language is correctly installed and working perfectly.

Node JS “Hello World” application

1. Install the Node JS on your system

Go to the official website and download the Node JS, then install it. Ubuntu users, follow this doc to install node js.

To verify the installation use the following two commands that checks node and npm versions.

node --version
npm --version
Check the Node and NPM version

2. Setup the app folder

After installing node js, create a new folder with the name you want (I named it 📁test) where we will write our first hello world application.

After that, go inside the newly created folder and initialize the NPM using the command – npm init -y

Now you can see that after executing npm init command a package.json file has been created in the folder –

node JS package.json file

This package.json file is used as a manifest, storing information about applications, modules, packages, and more.

3. Create index.js file

At the root of the application folder create the index.js file, and write a simple code inside the index.js, like the following –

Node index.js file

4. Run the index.js file on Node

Open your terminal or powershell and make sure the location is the same where the index.js file is present, then run the following command –

node index.js
run a JS file using node

5. Create your first Node.js web server

Node JS is built to run JavaScript on the server, so let’s see how we can create a server with the help of the http module.

You can also create a server using Express.js framework, and this will be the easiest way to create a server.

// Import Built-in HTTP Module
const http = require("http");

/* http://localhost:3000 */
const PORT = 3000;
const HOST = "localhost";

const server = http.createServer(function(request, response){
  response.statusCode = 200;
  response.setHeader("Content-Type", "text/html");
  response.write("<h1>Hello World!</h1>");
  response.end();
});

server.listen(PORT, HOST, function(){
  console.log(`Server is running - http://${HOST}:${PORT}`);
});

First, you have to install express.js. Use the following command to install the express framework –

npm i express
installing the express.js using npm
const express = require("express");
const app = express();

const PORT = 3000;

app.use(function(request, response){
  response.send("<h1>Hello World!</h1>");
});

app.listen(PORT, function(){
  console.log(`Server is running - http://localhost:${PORT}`);
});

Run the index.js

start the node js server

Here is the result

Open the URL in your browser - http://localhost:3000
node js hello world application runs on browser