[英]In Node.Js Express, does “res.render” end the http request?
So, only do "res.render" when you are sure that everything has finished, right?所以,只有当你确定一切都完成时才执行“res.render”,对吗? Because it ends the request and shoots out a webpage.
因为它结束了请求并弹出了一个网页。
If you don't provide a callback to res.render(view[, options[, fn]])
it will automatically give a response with 200 HTTP Status and Content-Type: text/html如果您不提供对
res.render(view[, options[, fn]])
的回调,它将自动给出 200 HTTP Status and Content-Type: text/html 的响应
res.render('view', {}, function() {
while (true); // should block
});
res.render(view[, options[, fn]])
res.render(view[, options[, fn]])
Render view with the given options and optional callback fn.
使用给定的选项和可选的回调 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.
当回调 function 被给出时,不会自动做出响应,否则会给出 200 和 text/html 的响应。
With the current github master commit , this is res.render
in lib/view.js :使用当前的github 主提交,这是lib/view.js 中的 res.render
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);
}
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.