node.js - express中app.render和res.render方法有哪些具体的区别?
迷茫
迷茫 2017-04-17 13:21:56
0
1
620

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

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
Peter_Zhu

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);
    });
};
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template