How to render a basic HTML view with Express and JavaScript?

Spread the love

Sometimes, we want to render a basic HTML view with Express and JavaScript.

In this article, we’ll look at how to render a basic HTML view with Express and JavaScript.

How to render a basic HTML view with Express and JavaScript?

To render a basic HTML view with Express and JavaScript, we use the ejs package.

To install it, we run

npm install ejs --save

Then we write

app.set("views", __dirname + "/views");
app.engine("html", require("ejs").renderFile);

app.get("/about", (req, res) => {
  res.render("about.html");
});

to set the view engine to ejs and the render about.html in the views folder in the /about route handler.

Conclusion

To render a basic HTML view with Express and JavaScript, we use the ejs package.

Leave a Reply

Your email address will not be published.