Full-Stack

Getting Started with Node.js: Your First Application

A Hello World program is the first application to write when learning a new language or framework. This software shows the Hello World message. In addition to demonstrating the language’s fundamental syntax, this checks to see if the language compiler installation was completed correctly. We will create a Hello World application using Node.js in this chapter.

Application for a Console

There is a command-line interface for Node.js. You can also run JavaScript code outside of a browser by using the Node.js runtime. As a result, any JavaScript code can be executed using the Node.js executable in a command terminal.

Save the following single line JavaScript as hello.js file.

console.log("Hello World");

Open a powershell (or command prompt) terminal in the folder in which hello.js file is present, and enter the following command −

PSD:\nodejs> node hello.js
Hello World

The Hello World message is displayed in the terminal.

Creating Node.js Application

The following three crucial elements are required in order to develop a “Hello, World!” web application with Node.js:

Step 1 – Import Required Module

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

var http =require("http");

Step 2 – Create Server

We use the created http instance and call http.createServer() method to create a server instance and then we bind it at port 3000 using the listen method associated with the server instance. Pass it a function with parameters request and response.

The createserver() method has the following syntax −

http.createServer(requestListener);

The requestlistener parameter is a function that executes whenever the server receives a request from the client. This function processes the incoming request and forms a server reponse.

The requestlistener function takes request HTTP request and response objects from Node.js runtime, and returns a ServerResponse object.

const http = require('http');

// Create a server
const server = http.createServer((req, res) => {
// Set response header
res.writeHead(200, { 'Content-Type': 'text/html' });

// Send response body
res.end('<h2 style="text-align: center;">Hello World</h2>');
});

// Server listens on port 3000
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});

The above function adds the status code and content-type headers to the ServerResponse, and Hello World message.

This function is used as a parameter to createserver() method. The server is made to listen for the incoming request at a particular port (let us assign 3000 as the port).

Step 3 – Testing Request & Response

Write the sample implementation to always return “Hello World”. Save the following script as hello.js.

const http = require('node:http');

// Define the request listener function
const listener = (req, res) => {
// Set HTTP status and content type
res.writeHead(200, { ‘Content-Type’: ‘text/html’ });

// Send response body
res.end('<h2 style="text-align: center;">Hello World</h2>');

};

// Create an HTTP server
const server = http.createServer(listener);

// Define the port number
const PORT = 3000;

// Start the server
server.listen(PORT, () => {
console.log(Server running at http://127.0.0.1:${PORT}/);
});

The application enters listen mode at port 3000 and launches the Node.js server on localhost. Now launch a browser and type the URL http://127.0.0.1:3000/. The Hello World message appears in the browser as intended.

It might be helpful:

What’s New in Node.js 21/22? 

Best Practices for API Design in Full-Stack Development

What is REST API with an example?

Exit mobile version