How to end an express.js / node POST response?

前端 未结 7 1633
情歌与酒
情歌与酒 2020-12-28 12:51

Im trying to just stop the post request after I\'ve saved a document to the DB, ie:

app.post(\'/blah\'), function(req,res){ 
//my code does a bunch of stuff
         


        
相关标签:
7条回答
  • 2020-12-28 13:04

    You can use res.end and pass in a string that you want to be sent to the client:

    res.end('It worked!');
    

    Alternatively, you could render a view and then end the response:

    res.render('blah.jade');
    res.end();
    

    All that said, redirecting the client is actually the best practice. It makes it so that when they hit the back button in their browser, they can move back seamlessly without getting any "POST required" popups or the like. This is the POST/redirect pattern and you can read more about it at http://en.wikipedia.org/wiki/Post/Redirect/Get.

    0 讨论(0)
  • 2020-12-28 13:05

    The client browser is waiting for a response. So why not give it one with something like res.send('<p>Thank you</p>'); or a nice rendered view?

    The reason res.end() sends the client to a blank page is that you aren't giving the client anything to display.

    0 讨论(0)
  • 2020-12-28 13:09

    If you could modify your client side code to make the post request via AJAX, you could simply use res.end().

    The problem here is that when the browser makes a POST request (not via AJAX), it no longer cares about the current page. It is expecting a new page, so it will load a new page. This is purely with the browser, and I do not believe there is currently a way for you to work around it.

    Overall, you should just use an AJAX POST request if you can.

    0 讨论(0)
  • 2020-12-28 13:13

    While you can use the underlying end method borrowed from Node's http module, Express.js has its own send method that calls end when appropriate:

    /**
     * Send a response.
     *
     * Examples:
     *
     *     res.send(new Buffer('wahoo'));
     *     res.send({ some: 'json' });
     *     res.send('<p>some html</p>');
     *     res.send(404, 'Sorry, cant find that');
     *     res.send(404);
     *
     * @param {Mixed} body or status
     * @param {Mixed} body
     * @return {ServerResponse}
     * @api public
     */
    res.send = function(body){
      .
      .
      .
    
      // respond
      this.end(head ? null : body);
      return this;
    };
    
    0 讨论(0)
  • 2020-12-28 13:24

    The best way to end the express routing without routing to new page or what ever the mess express want you not to stay on the current page although you have your own reason is to use window.stop, for example a form, you use javascript to handle the submission, the timeout should be enough to ensure the data is send as POST method.

    document.myform.mysubmit.click()
        setTimeout(function(){ 
            window.stop() 
        }, 3000);
    
    0 讨论(0)
  • 2020-12-28 13:25

    For future reference, you can also do:

    res.redirect('back');
    

    Basically this just redirects the browser back to the screen the request came from, in other words "refreshing" it. It's hacky and I wish there was a better way, but it works.

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