Sling Academy
Home/Node.js/How to Render HTML Views in Express.js

How to Render HTML Views in Express.js

Last updated: December 28, 2023

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.

Next Article: Fixing ERR_HTTP_HEADERS_SENT in Node.js and Express

Previous Article: Express JS: How to Serve Images & Static Files

Series: Node.js & Express Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates