9951 explained code solutions for 126 technologies


expressjsHow do I use Express.js and Handlebars together?


Express.js and Handlebars are a powerful combination for creating dynamic web applications. Here is an example of how to use them together:

// Setup Express
const express = require('express');
const app = express();

// Setup Handlebars
const exphbs  = require('express-handlebars');
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');

// Set up routes
app.get('/', (req, res) => {
    res.render('home', {
        title: 'My Home Page'
    });
});

// Start server
app.listen(3000, () => {
    console.log('Server is up on port 3000');
});

This code will set up an Express server on port 3000, and set up Handlebars as the view engine. The route / will render the file home.handlebars, with the title My Home Page.

The code consists of the following parts:

  1. Require Express and setup an Express app: const express = require('express'); const app = express();
  2. Require Express Handlebars and set it up as the view engine: const exphbs = require('express-handlebars'); app.engine('handlebars', exphbs()); app.set('view engine', 'handlebars');
  3. Setup a route to render a view: app.get('/', (req, res) => { res.render('home', { title: 'My Home Page' }); });
  4. Start the server: app.listen(3000, () => { console.log('Server is up on port 3000'); });

For more information about using Express and Handlebars together, see the Express Handlebars documentation.

Edit this code on GitHub