在Node.js Express中,“ res.render”是否结束HTTP请求?
-
26-10-2019 - |
题
因此,只有当您确定一切都已经完成时,只有“ res.render”,对吗?因为它结束了请求并发布了一个网页。
解决方案
如果您不提供回调 res.render(view[, options[, fn]])
它将自动提供200 HTTP状态和内容类型的响应:文本/HTML
res.render('view', {}, function() {
while (true); // should block
});
res.render(查看[,options [,fn]])
使用给定选项和可选回调fn渲染视图。当给出回调函数时,不会自动做出响应,但是否则给出了200和文本/html的响应。
其他提示
与电流 Github主人提交, , 这是 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);
}
}
};
不隶属于 StackOverflow