How To Create a Simple HTTP Server in Node?
Last Updated :
19 Feb, 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.
What is HTTP?
HTTP (Hypertext Transfer Protocol) is a protocol used for transferring data across the web. It's the foundation of any data exchange on the Web and it allows browsers and servers to communicate. HTTP servers handle incoming requests from clients (typically web browsers), process them, and send back appropriate responses.
In NodeJS, the built-in http module makes it easy to create an HTTP server that can handle these requests.
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.
Similar Reads
How to Create HTTPS Server with Node.js ? Creating an HTTPS server in Node.js ensures secure communication between your server and clients. HTTPS encrypts data sent over the network, providing a layer of security essential for handling sensitive information. This guide will walk you through the process of setting up an HTTPS server in Node.
4 min read
How to Create a Simple Server in Node.js that Display Hello World ? We will create a simple server in Node.js that returns Hello World using an express server. Node.js is a powerful JavaScript runtime built on Chrome's V8 engine, commonly used to build scalable network applications. One of the fundamental tasks when learning Node.js is creating a simple server that
2 min read
How to Create a Simple HTTP Server Listening at Port 3000 to Serve Video ? Creating a simple HTTP server to serve a video file can be done using various programming languages and frameworks. For simplicity, we'll use Python, which has built-in modules that make this task straightforward. In this article, weâll guide you through setting up a basic HTTP server using Python t
3 min read
How to Build a Simple Web Server with Node.js ? Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and itâs not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make
3 min read
How to Create a Simple Server Using ExpressJS? The server plays an important role in the development of the web application. It helps in managing API requests and communication between the client and the backend. ExpressJS is the fast and famous framework of the Node.Js which is used for creating the server.In this article, we will create a simp
3 min read
How to make HTTP requests in Node ? In the world of REST API, making HTTP requests is the core functionality of modern technology. Many developers learn it when they land in a new environment. Various open-source libraries including NodeJS built-in HTTP and HTTPS modules can be used to make network requests from NodeJS.There are many
4 min read
Node.js http.ServerResponse.socket Api The httpServerResponse.socket is an inbuilt application programming interface of class ServerResponse within http module which is used to get the reference of the underlying socket object. Syntax: response.socket Parameters: This method does not accept any arguments as a parameter. Return Value: Thi
2 min read
How HTTP POST requests work in Node ? The HTTP POST method is used to send data from the client to the server. Unlike GET, which appends data in the URL, POST sends data in the request body, which makes it ideal for form submissions, file uploads, and secure data transfers.In Node.js, handling POST requests is commonly done using the Ex
2 min read
How to Connect to Telnet Server from Node.js ? Connecting to a Telnet server from Node.js involves creating a client that can communicate using the Telnet protocol. Telnet is a protocol used to establish a connection to a remote host to execute commands and transfer data. In Node.js, you can use various packages to facilitate the connection to a
4 min read
How to Get POST Data in Node ? Handling POST data is a fundamental aspect of developing web applications and APIs using Node.js. POST requests are used when a client needs to send data to the server, such as submitting form data, uploading files, or sending JSON payloads. This article will cover various methods to retrieve and ha
4 min read