How to end an express.js / node POST response?
Use --> res.status(204).send(); inside the post request
if(err) {
console.log(err);
} else {
res.status(204).send();
}
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.
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;
};
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.