Express app.render() Method

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 4 Code Examples
Express.js

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 from res.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()

MethodOutput behaviorTypical use
app.render()Returns rendered HTML in callbackCustom handling flow
res.render()Renders and usually sends responseStandard 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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

4 people found this page helpful