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

前端 未结 7 1697
情歌与酒
情歌与酒 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.

提交回复
热议问题