9951 explained code solutions for 126 technologies


expressjsHow do I render a template using Express.js?


To render a template using Express.js, you need to set up a view engine such as Pug or EJS. After you have done that, you can use res.render() to render a template.

// Example
app.get('/', function (req, res) {
  res.render('index', { title: 'My App' });
});

This will render the index template with the title variable set to My App.

Code explanation

  • app.get(): Used to set up a route for the application
  • res.render(): Used to render a template with the given parameters

Helpful links

Edit this code on GitHub