Creating a Simple HTTP Server in Node
Last Updated :
08 Sep, 2025
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:
output Web Browser Output: When you access http://localhost:3000/ in your web browser, the server responds with
Create a Simple HTTP Server in NodeNow a simple HTTP server is created. You can enhance it to handle more complex use cases like CRUD operations etc.
What does the http.createServer() method do in a NodeJS application?
-
Starts the server and listens on a port
-
Creates an HTTP server and returns a server object
-
Sends the response to the client
-
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?
-
-
To define the HTTP status code and headers for the response
-
-
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?
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?
-
Node displays a JSON response
-
The terminal prints the message "Hello, GeeksforGeeks!"
-
The browser displays "Hello, GeeksforGeeks!"
-
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
Introduction & Installation
Node.js Modules , Buffer & Streams
Node.js Asynchronous Programming
Node.js NPM
Node.js Deployments & Communication
Resources & Tools