Node.js Web Server with HTML Response

Node.js can create a web server and send an HTML page as the response.

Node.js Code

const http = require("http");

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

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

    res.end(`
        <h1>Welcome to My Node.js Server</h1>
        <p>This HTML is sent by Node.js.</p>
    `);
});

server.listen(3000);
        
Browser Output:

Welcome to My Node.js Server
This HTML is sent by Node.js.

URL: http://localhost:3000

Steps

  1. Import the http module.
  2. Create the server.
  3. Set the response type to HTML.
  4. Send HTML using res.end().
  5. Start the server on port 3000.
← Back to Home