In Node.Js Express, does “res.render” end the http request?

前端 未结 2 1325
后悔当初
后悔当初 2021-02-15 13:09

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

相关标签:
2条回答
  • 2021-02-15 13:24

    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);
        }
      }
    };
    
    0 讨论(0)
  • 2021-02-15 13:31

    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

    0 讨论(0)
提交回复
热议问题