---
title: "How can you create a simple HTTP server using Node.js?"  
description: "How can you create a simple HTTP server using Node.js?"  
author: "Revati S Misra"  
published: 2023-09-27  
updated: 2023-09-28  
canonical: https://www.mindstick.com/forum/159958/how-can-you-create-a-simple-http-server-using-node-js  
category: "node.js"  
tags: ["javascript", "node js", "http"]  
reading_time: 3 minutes  

---

# How can you create a simple HTTP server using Node.js?

How can you create a [simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) [HTTP server](https://www.mindstick.com/forum/160008/how-to-create-a-simple-http-server-using-node-js) using [Node.js](https://www.mindstick.com/articles/1499/upload-and-download-file-in-node-js)?

## Replies

### Reply by Aryan Kumar

Creating a simple [HTTP](https://www.mindstick.com/blog/217/http-endpoints-in-sql-server-2005-2008) [server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) using Node.js is relatively straightforward. Node.js has a built-in **http** module that allows you to create and configure HTTP servers easily. Here's a step-by-step guide to create a basic HTTP server in Node.js:

## Import the http Module:

Start by requiring the **http** module, which is a core module in Node.js.

## Create an HTTP Server:

Use the **http.createServer()** method to create an HTTP server. You need to pass a callback function that will be executed whenever a request is received by the server. This function receives two arguments: **request** (an instance of **http.IncomingMessage**) and **response** (an instance of **http.ServerResponse**).

## Handle Incoming Requests:

Inside the callback function, you can implement logic to handle incoming HTTP requests. For a simple example, let's send a "Hello, World!" response for all requests.

## Specify the Port and Start the Server:

Choose a port number on which the server should listen for incoming requests (e.g., 3000). Then, use the **server.listen()** method to start the server. The **console.log()** statement is used to log a message indicating that the server is running.

## Run the Server:

Save your Node.js file (e.g., **server.js**) and run it using Node.js:

Your server will now be running on the specified port (e.g., http://localhost:3000).

## Access the Server:

Open a web browser or use a tool like **curl** or **httpie** to make HTTP requests to your server. You should see the "Hello, World!" response when you access the server's URL.

Now, You've created a simple HTTP server using Node.js. This is a basic example, but you can extend the server's functionality to handle more complex requests and responses as needed for your application.

```plaintext
const http = require('http');

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

const port = 3000;
server.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
```

To run this code:

- Save it to a file (e.g., **server.js**).
- Open your terminal and navigate to the directory where you saved the file.
- Run the server using Node.js:

```plaintext
node server.js
```

- Your server will be running on http://localhost:3000. You can access it using a web browser or a tool like **curl**.

This code creates a basic HTTP server that listens on port 3000 and responds with "Hello, World!" for all incoming requests. You can customize the response and add more routes and logic as needed for your specific application.


---

Original Source: https://www.mindstick.com/forum/159958/how-can-you-create-a-simple-http-server-using-node-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
