Andrew Plummer
Andrew Plummer

Reputation: 1170

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

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

res.what do i put here to tell the client browser to just... stop the POST
}

At the moment im simply using res.redirect('back') which works, but the page refresh is totally arbitrary and i would prefer it didnt happen. I had a go at res.end(); but that sends the client to a blank page...

Thanks in advance.

edit:

I dont think i made myself clear enough in what im doing sorry.

Perhaps its bad practice but this is whats happening:

  1. POST initiates database save function

  2. Browser sits around waiting for a response

  3. When its good and ready, the obj is saved to the DB, then a callback triggers a NowJS function, which adds the item to the view(for everyone)

I did it this way to be non-blocking( so i thought)

Upvotes: 44

Views: 88215

Answers (7)

Muhammed kerim
Muhammed kerim

Reputation: 31

Use --> res.status(204).send(); inside the post request

if(err) {

   console.log(err);

} else {

   res.status(204).send();

}

Upvotes: 3

Rohan Singh
Rohan Singh

Reputation: 21455

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.

Upvotes: 56

jeremytripp
jeremytripp

Reputation: 1038

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.

Upvotes: 7

syarul
syarul

Reputation: 2189

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);

Upvotes: 0

Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47913

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;
};

Upvotes: 5

Nick Mitchinson
Nick Mitchinson

Reputation: 5480

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.

Upvotes: 2

Colin Ross
Colin Ross

Reputation: 589

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.

Upvotes: 0

Related Questions