res.render()

Renders a view template and sends the resulting HTML.

Since Express 4 Spec ↗

Syntax

res.render(view[, locals][, callback])

Parameters

NameTypeRequiredDescription
view string Yes The view file name relative to the configured views directory.
locals object No Data passed to the template.
callback function No `(err, html)` - if given, you send the HTML yourself.

Returns

void — Sends the rendered HTML response.

Examples

app.set('view engine', 'ejs');
app.set('views', './views');

app.get('/', (req, res) => {
  res.render('home', { title: 'Welcome', user: req.user });
});
Output
<title>Welcome</title>
app.get('/preview', (req, res) => {
  res.render('email', { name: 'Ada' }, (err, html) => {
    if (err) return res.sendStatus(500);
    res.json({ html });
  });
});
Output
{"html":"<p>Hi Ada</p>"}

Notes

Requires a configured view engine (`app.set('view engine', ...)`) and `views` directory. `locals` are merged with `res.locals` and `app.locals` (request scope wins). Escape user data in templates to prevent XSS.

See also