node.js express.js 中的“app.render”和“res.render”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15403791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:51:37  来源:igfitidea点击:

What's the difference between "app.render" and "res.render" in express.js?

node.jsexpress

提问by Evan Carroll

Docs for app.render:

文档app.render

Render a view with a callback responding with the rendered string. This is the app-level variant of res.render(), and otherwise behaves the same way.

使用回调响应渲染的字符串来渲染视图。这是 的应用级变体res.render(),其他方面的行为方式相同。

Docs for res.render:

文档res.render

Render a view with a callback responding with the rendered string. When an error occurs next(err)is invoked internally. When a callback is provided both the possible error and rendered string are passed, and no automated response is performed.

使用回调响应渲染的字符串来渲染视图。发生错误时next(err)在内部调用。当提供回调时,可能的错误和呈现的字符串都会被传递,并且不会执行自动响应。

How can I figure out when to use which one?

我如何确定何时使用哪一个?

回答by zemirco

Here are some differences:

以下是一些差异:

  1. You can call app.renderon root leveland res.renderonly inside a route/middleware.

  2. app.renderalways returns the htmlin the callback function, whereas res.renderdoes so only when you've specified the callback function as your third parameter. If you call res.renderwithout the third parameter/callback function the rendered html is sent to the client with a status code of 200.

    Take a look at the following examples.

    • app.render

      app.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="/stylesheets/style.css"></head><body><h1>res vs app render</h1><p>Welcome to res vs app render</p></body></html>
      
    • res.renderwithout third parameter

      app.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 parameter

      app.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
      
  3. res.renderuses app.renderinternally to render template files.

  4. You can use the renderfunctions to create html emails. Depending on your structure of your app, you might not always have acces to the appobject.

    For example inside an external route:

    app.js

    var routes = require('routes');
    
    app.get('/mail', function(req, res) {
        // app object is available -> app.render
    })
    
    app.get('/sendmail', routes.sendmail);
    

    routes.js

    exports.sendmail = function(req, res) {
        // can't use app.render -> therefore res.render
    }
    
  1. 您可以app.render根级别调用,并且res.render只能在 route/middleware 内部调用。

  2. app.render总是html在回调函数中返回,而res.render只有当您将回调函数指定为第三个参数时才会这样做。如果您在res.render没有第三个参数/回调函数的情况下调用,则呈现的 html 将发送到客户端,状态代码为200.

    看看下面的例子。

    • app.render

      app.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="/stylesheets/style.css"></head><body><h1>res vs app render</h1><p>Welcome to res vs app render</p></body></html>
      
    • res.render没有第三个参数

      app.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.render带第三个参数

      app.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
      
  3. res.renderapp.render内部用于渲染模板文件。

  4. 您可以使用这些render功能来创建html 电子邮件。根据您的应用程序结构,您可能并不总是可以访问该app对象。

    例如在外部路由中:

    app.js

    var routes = require('routes');
    
    app.get('/mail', function(req, res) {
        // app object is available -> app.render
    })
    
    app.get('/sendmail', routes.sendmail);
    

    routes.js

    exports.sendmail = function(req, res) {
        // can't use app.render -> therefore res.render
    }
    

回答by VeXii

use app.renderin scenarios where you need to render a view but not send it to a client via http. html emails springs to mind.

app.render在需要渲染视图但不通过 h​​ttp 将其发送到客户端的场景中使用。html 电子邮件浮现在脑海中。

回答by Vinay Vemula

along with these two variants, there is also jade.renderFilewhich generates html that need not be passed to the client.

除了这两个变体,还有jade.renderFile生成不需要传递给客户端的 html。

usage-

用法-

var jade = require('jade');

exports.getJson = getJson;

function getJson(req, res) {
    var html = jade.renderFile('views/test.jade', {some:'json'});
    res.send({message: 'i sent json'});
}

getJson()is available as a route in app.js.

getJson()可作为 app.js 中的路由使用。