Creating a Basic HTTP Server (Without Framework)

http Module

Steps to Build a Basic HTTP Server

  1. Import the http module

    const http = require('http');
    
  2. Define the port number

    const PORT = 3000;
    
  3. Create the server instance

    const server = http.createServer();
    
  4. Start the server using listen()

    server.listen(PORT, () => {
      console.log(`Server is running at <http://localhost>:${PORT}`);
    });
    

What is localhost and the URL?

Example:

If your server is running on port 3000, you can access it by visiting:

<http://localhost:3000>

in your browser or using a tool like Postman.