“app.render”之间有什么区别?和“res.render”在express.js? [英] What's the difference between "app.render" and "res.render" in express.js?

查看:212
本文介绍了“app.render”之间有什么区别?和“res.render”在express.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

app.render 的文档:


使用回显字符串回调响应的视图。这是 res.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


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

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.

有人可以帮助我弄清楚何时使用哪一个?

Could someone please help me figure out when to use which one?

推荐答案

这里有一些区别:


  1. 您可以在根级别 res调用 app.render

app.render 总是返回回调函数中的 html ,而 res.render 只有当您将回调函数指定为第三个参数时,才执行此操作。如果您调用 res.render 而不使用第三个参数/回调函数,则将呈现的HTML发送到客户端,状态代码为$ code> 200

app.render always returns the html in the callback function, whereas res.render does so only when you've specified the callback function as your third parameter. If you call res.render without the third parameter/callback function the rendered html is sent to the client with a status code of 200.

查看以下示例。


  • app.render

  • 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 没有第三个参数

  • res.render without 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.render 与第三个参数

  • res.render with 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
    


  • res.render 使用

    res.render uses app.render internally to render template files.

    您可以使用 render 函数创建 html电子邮件。根据您的应用程序的结构,您可能并不总是访问应用程序对象。

    You can use the render functions to create html emails. Depending on your structure of your app, you might not always have acces to the app object.

    例如在外部路由中:

    app.js

    app.js

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






    .js


    routes.js

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


    这篇关于“app.render”之间有什么区别?和“res.render”在express.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆