Create reusable rendered content
Generate HTML for an email or another non-HTTP output before handing it to a delivery system.
Express.js · Module
app.render() renders a named Express view and provides the resulting HTML to a callback. Unlike res.render(), it does not send the rendered view to the client automatically.
app.render renders a named view using the application’s configured view settings and optional local variables. Its callback receives either an error or the generated HTML, so the caller decides what to do with the result.
app.render(view, [locals], callback)| Name | Type | Description |
|---|---|---|
view | String | Name of the view to render. |
locals | Object | undefined | Optional object whose properties define local variables for the view. |
callback | Function | Callback invoked as callback(err, html). |
The rendered HTML of a view is provided through the callback function.
const express = require('express');
const app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
app.get('/preview', (req, res, next) => {
app.render('email', { name: 'Ada' }, (err, html) => {
if (err) return next(err);
res.type('html').send(html);
});
});
app.listen(3000);Renders the configured email view to an HTML string, then sends that string explicitly.
Generate HTML for an email or another non-HTTP output before handing it to a delivery system.
Render a template into a string so application code can inspect or transform it before responding.
Produce a server-side preview while retaining control over when and how the HTML is sent.
app.render(view, locals, callback) generates a view’s HTML and passes it to the callback as callback(err, html). It is useful when the rendered output must be handled by application code rather than sent directly.
Follow-up: How does it relate to res.render()? res.render() uses app.render() internally, while app.render() itself only produces the rendered string.
The view argument is the name of the view to render. The optional locals object supplies properties that become local variables available to that view.
Follow-up: What should be done when rendering fails? Check err first and handle or forward it before using html.
The method returns rendered HTML through its callback rather than as a direct synchronous return value. This lets the caller choose whether to send, store, transform, or otherwise process the generated string.
Follow-up: Why might an application use this instead of res.render()? It can generate content for non-response work, such as composing an email or previewing output.
res.renderapp.setview engineviews