DEV Community

Tirth Raval
Tirth Raval

Posted on

Building a Robust HTTP Server with Node.js: A Step-by-Step Guide

What is an HTTP server?

HTTP server can be defined as software that can take clients' requests and deliver the web pages to the user as the response.

What is Node.js?

Node.js is JavaScript run time which allows user to run JavaScript on their local machine. User can create their own HTTP server using different frameworks.

Why do we want to create the HTTP servers?

The primary purpose of creating the HTTP servers is that, once we create the HTTP server, we want to expose it to the world so people can use it without worrying about the underlying concept.

One good example I can think of is ChatGPT. If we ask ChatGPT anything, it will make some network calls and generate a response. As users, we do not have to worry about how ChatGPT generates the response.

How can we create the HTTP server using Node.js?

Express.js is a framework that allows us to create HTTP servers using JavaScript as the programming language.

Let us understand the status code before jumping into code.

What is the status code?

It is a message that the server sends to the browser telling the status of the request.

Status Code Message
200 The Request succussed
404 The Server can not find the resource
500 Internal Server Error

For more details, visit:
https://www.geeksforgeeks.org/10-most-common-http-status-codes/#

Method Use
Get To receive the data from the Database.
Post To add a new entry into the Database.
Put To update an entry in the Database.
Delete To delete an entry from the Database.

For a better understanding of the types of requests, please visit: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

Let's make our own HTTP server using Express.js

Step 1: Initialize a new Node.js project.

Write the below commands in your terminal
npm i

Step 2: Install Express.js.

npm install express

Step 3: Initialize the index.js

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

//we can create the different server based on the request

app.listen(3000, () => {
console.log("Server is running on port 3000") 
})
Enter fullscreen mode Exit fullscreen mode

Step 5: Create your HTTP server based on the requests

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

//GET request handler
app.get('/api/resource', (req, res) => {
    // Your logic for handling GET request
    res.send('GET request received');
});

// POST request handler
app.post('/api/resource', (req, res) => {
    // Your logic for handling POST request
    res.send('POST request received');
});

// PUT request handler
app.put('/api/resource/:id', (req, res) => {
    const resourceId = req.params.id;
    // Your logic for handling PUT request
    res.send(`PUT request received for resource with ID: ${resourceId}`);
});

// DELETE request handler
app.delete('/api/resource/:id', (req, res) => {
    const resourceId = req.params.id;
    // Your logic for handling DELETE request
    res.send(`DELETE request received for resource with ID: ${resourceId}`);
});

app.listen(3000, () => {
console.log("Server is running on port 3000") 
})
Enter fullscreen mode Exit fullscreen mode

req stands for request and represents the HTTP request from the client to the server.

res stands for response and represents the HTTP response that the server sends back to the client.

Thank you Post

Top comments (0)