javascript 在 Node.Js Express 中,“res.render”是否结束了 http 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5814897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Node.Js Express, does "res.render" end the http request?
提问by TIMEX
So, only do "res.render" when you are sure that everything has finished, right? Because it ends the request and shoots out a webpage.
所以,只有当你确定一切都已经完成时才执行“res.render”,对吗?因为它结束请求并发出一个网页。
回答by mak
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 和 Content-Type: text/html 的响应
res.render('view', {}, function() {
while (true); // should block
});
res.render(view[, options[, fn]])
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.
res.render(view[, options[, fn]])
使用给定的选项和可选的回调 fn 渲染视图。当给出回调函数时,不会自动做出响应,否则会给出 200 和 text/html 的响应。
回答by entropo
With the current github master commit, this is res.render
in lib/view.js:
使用当前的github master commit,这是res.render
在lib/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);
}
}
};