How to render basic HTML view with Express?

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

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

How to render basic HTML view with Express?

To render basic HTML view with Express, we call app.use with express.static to serve a static folder.

For instance, we write

const express = require('express');
const app = express.createServer();

app.set("view options", {
  layout: false
});
app.use(express.static(__dirname + '/public'));

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

app.listen(8080, '127.0.0.1')

to call express.static with the static folder path in our project to serve that as the static folder.

Then we call res.render with a path to a file in the static folder to render that as the view content.

Conclusion

To render basic HTML view with Express, we call app.use with express.static to serve a static folder.