2011-04-28 44 views

回答

11

如果不提供回調res.render(view[, options[, fn]])它會自動給200 HTTP狀態和Content-Type的響應:text/html的

res.render('view', {}, function() { 
    while (true); // should block 
}); 

res.render(查看[,選項[ ,fn]])

使用給定的選項和可選的回調函數fn來渲染視圖。當一個回調函數被給出時,響應將不會自動進行,但是另外會給出200和text/html的響應。

express.js guide

4

隨着當前github master commit,這是res.renderlib/view.js

/** 
* Render `view` with the given `options` and optional callback `fn`. 
* When a callback function is given a response will _not_ be made 
* automatically, however otherwise a response of _200_ and _text/html_ is given. 
* 
* Options: 
* 
* - `scope`  Template evaluation context (the value of `this`) 
* - `debug`  Output debugging information 
* - `status` Response status code 
* 
* @param {String} view 
* @param {Object|Function} options or callback function 
* @param {Function} fn 
* @api public 
*/ 
res.render = function(view, opts, fn, parent, sub){ 
    // support callback function as second arg 
    if ('function' == typeof opts) { 
    fn = opts, opts = null; 
    } 

    try { 
    return this._render(view, opts, fn, parent, sub); 
    } catch (err) { 
    // callback given 
    if (fn) { 
     fn(err); 
    // unwind to root call to prevent 
    // several next(err) calls 
    } else if (sub) { 
     throw err; 
    // root template, next(err) 
    } else { 
     this.req.next(err); 
    } 
    } 
};