在 Node.Js Express 中,“res.render”是否有效?结束http请求?

发布于 2024-11-03 15:33:18 字数 58 浏览 1 评论 0原文

因此,只有在确定一切都已完成时才执行“res.render”,对吗?因为它结束了请求并弹出了一个网页。

So, only do "res.render" when you are sure that everything has finished, right? Because it ends the request and shoots out a webpage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中二柚 2024-11-10 15:33:18

如果您不提供对 res.render(view[, options[, fn]]) 的回调,它将自动给出包含 200 HTTP Status 和 Content-Type: text/html 的响应

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

res.render(视图[, 选项[, fn]])

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

express.js 指南

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', {}, 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.

express.js guide

一袭白衣梦中忆 2024-11-10 15:33:18

对于当前的 github 主提交,这是 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);
    }
  }
};

With the current github master commit, this is res.render in 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);
    }
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文