在Node.Js Express中,“res.render”会结束http请求吗?

时间:2011-04-28 07:03:50

标签: javascript http node.js express

所以,当你确定一切都已完成时,只做“res.render”吧?因为它结束了请求并拍摄了一个网页。

2 个答案:

答案 0 :(得分:11)

如果您没有向res.render(view[, options[, fn]])提供回叫,它会自动提供200 HTTP状态和内容类型的响应:text / html

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

res.render(查看[,选项[,fn]])

     

使用给定选项和可选回调fn渲染视图。当给出回调函数时,不会自动响应,否则会给出200和text / html的响应。

express.js guide

答案 1 :(得分:4)

使用当前github master commit,这是lib/view.js中的res.render

 /**
 * 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);
    }
  }
};