How to Render HTML Views in Express.js

Updated: December 28, 2023 By: Guest Contributor Post a comment

Learn some different techniques to render HTML views in Express.js.

Solution 1: Using EJS Templating Engine

Description: EJS is an embedded JavaScript templating engine that lets you generate HTML markup with plain JavaScript. It’s easy to use with Express and allows for embedding JavaScript code directly within your HTML templates.

  • Install EJS using npm: npm install ejs.
  • Set EJS as the view engine in your Express app.
  • Create an EJS template file.
  • Render the view by passing the template name and data to the res.render method in your route handler.
const express = require('express');
const app = express();
app.set('views', './views');
app.set('view engine', 'ejs');

app.get('/', (req, res) => {
    res.render('index', { title: 'EJS Example' });
});

app.listen(3000, () => console.log('Server started on port 3000'));

Pros: Easy to use, allows JavaScript to be embedded within HTML templates, large community support.

Cons: Additional syntax to learn, can become complex for large-scale applications.

Solution 2: Sending Plain HTML with res.sendFile

Description: For simple use cases where templating is not required, sending a plain HTML file using res.sendFile can be the easiest solution. This does not involve any templating engine and serves a static HTML file directly to the client.

  • Create a static HTML file.
  • Use the res.sendFile method in your route handler to serve the HTML file, making sure to specify the absolute path.
const express = require('express');
const app = express();
const path = require('path');

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(3000, () => console.log('Server started on port 3000'));

Pros: Straightforward method, no additional setup, good for serving static content.

Cons: Not suitable for dynamic content, no templating engine features.

Solution 3: Serving Dynamic HTML with Mustache

Description: Mustache is a logic-less template syntax that can be used for HTML, config files, source code, etc. With its simplicity, Mustache is great for projects that require minimalistic templating.

  • Install Mustache via npm: npm install mustache-express.
  • Set Mustache as the view engine in your Express app.
  • Create a Mustache template file.
  • Render the view by passing the template name and data to the res.render method in your route handler.
const express = require('express');
const app = express();
const mustacheExpress = require('mustache-express');

app.engine('mustache', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');

app.get('/', (req, res) => {
    res.render('index.mustache', { title: 'Mustache Example' });
});

app.listen(3000, () => console.log('Server started on port 3000'));

Pros: Simple syntax, logic-less templates, supports partials.

Cons: Can be too minimalistic for complex applications, limited control flow mechanisms.

Conclusion

There are multiple ways to render HTML in Express.js depending on your project’s needs. EJS provides a powerful yet easy-to-use templating engine, res.sendFile is perfect for serving static HTML files, and Mustache offers a minimalistic approach to templating. Choosing the right method will depend on whether your content is static or dynamic and the complexity of the HTML content you are rendering.