Here are some differences:
-
You can call
app.renderon root level andres.renderonly inside a route/middleware. -
app.renderalways returns thehtmlin the callback function, whereasres.renderdoes so only when you’ve specified the callback function as your third parameter. If you callres.renderwithout the third parameter/callback function the rendered html is sent to the client with a status code of200.Take a look at the following examples.
-
app.renderapp.render('index', {title: 'res vs app render'}, function(err, html) { console.log(html) }); // logs the following string (from default index.jade) <!DOCTYPE html><html><head><title>res vs app render</title><link rel="stylesheet" href="https://stackoverflow.com/stylesheets/style.css"></head><body><h1>res vs app render</h1><p>Welcome to res vs app render</p></body></html> -
res.renderwithout third parameterapp.get('/render', function(req, res) { res.render('index', {title: 'res vs app render'}) }) // also renders index.jade but sends it to the client // with status 200 and content-type text/html on GET /render -
res.renderwith third parameterapp.get('/render', function(req, res) { res.render('index', {title: 'res vs app render'}, function(err, html) { console.log(html); res.send('done'); }) }) // logs the same as app.render and sends "done" to the client instead // of the content of index.jade
-
-
res.renderusesapp.renderinternally to render template files. -
You can use the
renderfunctions to create html emails. Depending on your structure of your app, you might not always have acces to theappobject.For example inside an external route:
app.jsvar routes = require('routes'); app.get('/mail', function(req, res) { // app object is available -> app.render }) app.get('/sendmail', routes.sendmail);
routes.jsexports.sendmail = function(req, res) { // can't use app.render -> therefore res.render }