search

Home  >  Q&A  >  body text

node.js - express中app.render和res.render方法有哪些具体的区别?

我看了官方的api文档,但还是有点不明白,希望大家能够给我通俗点的解释,谢谢^_^

迷茫迷茫2867 days ago652

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:23:56

    What the document says is actually pretty good

    Think of app.render() as a utility function for generating rendered view strings. Internally res.render() uses app.render() to render views.

    treats app.render as a tool for generating views, and res.render also calls app.render internally.

    The difference is this, app.render is only responsible for generating the view. You will find that it is not capable of responding to the view to the client (browser). Only res.render has the response object in hand and can respond to the view. to the client.

    The pseudo code of

    res.render can be seen as follows:

    res.render = function(view, locals, cb){
        app.render(view, locals, function(err, html){
            if(typeof cb !== 'undefined'){
                return cb(err, html);
            }
            res.send(html);
        });
    };

    reply
    0
  • Cancelreply