Categories
JavaScript Answers

How to generate HTML with Node.js?

Spread the love

To generate HTML with Node.js, we call the createServer method.

For instance, we write

const http = require("http");

http
  .createServer((req, res) => {
    res.write("<html><head></head><body>");
    res.write("<p>Write your HTML content here</p>");
    res.end("</body></html>");
  })
  .listen(1337);

to call createServer with a callback that calls res.write to write HTML as the response body.

We call res.end to finish writing the response body.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *