Express app.render() Method
What you’ll learn
- How to render templates using
app.render(view, [locals], callback). - How to use callback output for custom response flows.
- How
app.render()differs fromres.render(). - How to avoid common rendering setup and callback mistakes.
Overview
app.render() lets you compile a view template into an output string, then decide how to use that output in code.
String-first rendering
Receive rendered output in callback for further processing.
Custom delivery flows
Useful for emails, logging, caching, or manual response handling.
Engine dependent
Requires proper view engine and template directory configuration.
Syntax
javascript
app.render(view, [locals], callback)- view: template name (without extension when default view engine is set).
- locals: optional data object available inside template.
- callback: receives
(err, html)after rendering.
1
Basic app.render() usage
javascript
app.render('welcome', { name: 'CodeToFun' }, function (err, html) {
if (err) return console.error(err);
console.log(html);
});2
Render then send manually
javascript
app.get('/preview', function (req, res, next) {
app.render('preview', { mode: 'manual' }, function (err, html) {
if (err) return next(err);
res.type('html').send(html);
});
});📋 app.render() vs res.render()
| Method | Output behavior | Typical use |
|---|---|---|
app.render() | Returns rendered HTML in callback | Custom handling flow |
res.render() | Renders and usually sends response | Standard route response rendering |
🧪 Testing checklist
- Verify rendered output string is produced for valid templates.
- Test missing template case and ensure callback error handling works.
- Confirm locals values are reflected correctly in rendered output.
- Check manual response flow when using rendered HTML with
res.send().
Pitfalls to avoid
No error handling
Silent failures
Always handle err in render callback.
Wrong template path
View not found
Ensure views directory and filenames are correctly configured.
Method confusion
Unexpected response behavior
Use res.render() when you want Express to send response directly.
❓ FAQ
It renders a view template and returns the result through a callback, instead of directly sending a response.
app.render() renders to a string via callback, while res.render() usually renders and sends the output to the client.
Use it when you need rendered HTML for custom delivery flows, emails, logging, or post-processing before response.
Yes. You still need view engine and views path configuration for template resolution.
Yes. Provide a locals object as the second argument before the callback.
Summary
- Core use:
app.render()renders a view into HTML string via callback. - Difference: unlike
res.render(), it does not auto-send response. - Practice: handle callback errors and keep view configuration correct.
Did you know?
app.render(view, [locals], callback) renders a view to a string without automatically sending it in the HTTP response.
4 people found this page helpful
