Node.js HTTP Module

The http module is used to create a web server using Node.js.

Example

const http = require("http");

const server = http.createServer((req, res) => {

    res.writeHead(200, {
        "Content-Type": "text/html"
    });

    res.end("Hello from Node.js Server!");
});

server.listen(3000);
        
Server Output:

Hello from Node.js Server!

Open in browser:
http://localhost:3000

Explanation

  1. Import the http module.
  2. Create a server using createServer().
  3. Send a response using res.end().
  4. Start the server using listen().
  5. Open localhost in the browser.
← Back to Home