Open In App

Creating a Simple HTTP Server in Node

Last Updated : 08 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

NodeJS is a powerful runtime environment that allows developers to build scalable and high-performance applications, especially for I/O-bound operations. One of the most common uses of NodeJS is to create HTTP servers.It handles incoming requests from clients (typically web browsers), processes them, and sends back appropriate responses.

Steps to Create a NodeJS Server

To create a simple HTTP server in NodeJS, follow these steps:

Step 1: Initialize the Project

Begin by initializing your project using npm, which will create a package.json file to manage your project's dependencies and configurations.

npm init -y

Step 2: Import the HTTP Module

NodeJS includes a built-in HTTP module that allows you to create an HTTP server.

const http = require('http');

Step 3: Create a Server

Use the http.createServer() method to create an HTTP server. This method accepts a callback function that handles incoming requests and sends responses.

const server = http.createServer((request, response) => {
// Request handling logic
});

Step 4: Handle Requests

Within the server, set the response header and body to define how the server responds to incoming requests.

const server = http.createServer((request, response) => {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end('Hello, World!\n');
});

Example: After implementing the above steps, we will effectively establish the NodeJS server

JavaScript
const http = require('http');
const server = http.createServer((request, response) => {
    response.writeHead(200, { 'Content-Type': 'text/plain' });
    response.end('Hello, GeeksforGeeks!\n');
});
const PORT = 3000;
server.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}/`);
});
  • Loading the http Module: The http module is required to create an HTTP server.
  • Creating the Server: The createServer method is used to create the server, which takes a callback function that handles incoming requests and sends responses.
  • Setting Response Headers and Body: The writeHead method sets the HTTP status code and headers, while the end method sends the response body.
  • Starting the Server: The listen method starts the server on the specified port and IP address, and a callback function logs a message when the server is running.

Terminal Output: When you start the server, you'll see:

Terminal output
output

Web Browser Output: When you access http://localhost:3000/ in your web browser, the server responds with

Web Browser output
Create a Simple HTTP Server in Node

Now a simple HTTP server is created. You can enhance it to handle more complex use cases like CRUD operations etc.

Suggested Quiz
4 Questions

What does the http.createServer() method do in a NodeJS application?

  • A

    Starts the server and listens on a port

  • B

    Creates an HTTP server and returns a server object

  • C

    Sends the response to the client

  • D

    Imports the HTTP module

Explanation:

http.createServer() creates a new HTTP server instance. It does not start listening automatically; you still need to call server.listen().

What is the purpose of the response.writeHead() method in an HTTP server?

  • A

    To start the server

  • B

    To define the HTTP status code and headers for the response

  • C

    To close the server

  • D

    To handle incoming requests

Explanation:

writeHead(statusCode, headers) sets the response metadata. For example:
response.writeHead(200, { 'Content-Type': 'text/plain' });

Which of the following correctly starts a NodeJS HTTP server on port 3000?

  • A

    server.run(3000)

  • B

    server.execute(3000)

  • C

    server.listen(3000)

  • D

    server.start(3000)

Explanation:

server.listen(PORT) tells Node to begin listening for incoming HTTP requests. This is essential to activate the server.

What happens when you access http://localhost:3000/ after starting the example server?

  • A

    Node displays a JSON response

  • B

    The terminal prints the message "Hello, GeeksforGeeks!"

  • C

    The browser displays "Hello, GeeksforGeeks!"

  • D

    The server shuts down automatically

Explanation:

The server sends response.end('Hello, GeeksforGeeks!\n'); which appears in the browser when visiting the server URL.

Quiz Completed Successfully
Your Score :   2/4
Accuracy :  0%
Login to View Explanation
1/4 1/4 < Previous Next >

Explore