Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

HTTP Server with Node.js

HTTP Server with Node.js

Creating an HTTP server in Node.js is a fundamental task that allows you to handle HTTP requests and serve web content. You can build various types of web applications, including APIs, web servers, and more. Here's a step-by-step guide to creating an HTTP server in Node.js:

Step 1: Import the Required Modules

First, you need to import the built-in http module, which provides the necessary functions to create an HTTP server

const http = require('http');
 

Step 2: Create the Server

Use the http.createServer() method to create an HTTP server. You'll need to define a callback function that will be called for each incoming HTTP request. This function should take two arguments: a request (req) and a response (res) object.

const server = http.createServer((req, res) => {
  // Handle incoming requests here
});
 

Step 3: Handle Incoming Requests

Within the callback function, you can handle incoming HTTP requests. For example, you can set the response headers, write content to the response, and end the response.

Here's a simple example that responds with "Hello, World!" to all incoming requests:

const server = http.createServer((req, res) => {
  // Set response headers
  res.setHeader('Content-Type', 'text/plain');
  
  // Write content to the response
  res.write('Hello, World!');
  
  // End the response
  res.end();
});
 

Step 4: Start the Server

After creating the server, you need to specify the port number and host on which the server should listen. Commonly, Node.js servers listen on port 3000 or other available ports.

const port = 3000;
const hostname = '127.0.0.1';

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
 

Step 5: Running the Server

You can run the server script using Node.js. Open your terminal, navigate to the directory where your script is located, and execute the following command.

node server.js

 

Step 6: Accessing the Server

Once the server is running, you can access it in a web browser or make HTTP requests using tools like cURL or Postman. By default, the server will respond to requests at the specified hostname and port (e.g., http://127.0.0.1:3000/).

Step 7: Handling Different Routes (Optional)

For more advanced web applications, you can use frameworks like Express.js to handle different routes, HTTP methods, and more complex request processing.

Here's a simple example using Express.js:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

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

This example defines a route for the root URL (/) and responds with "Hello, World!" when accessed.

That's it! You've created a basic HTTP server in Node.js. You can build upon this foundation to create more complex web applications and APIs.