18

I have been searching all over for how to do this - I am trying to redirect after a DELETE request is made - here is the code I am using WITHOUT THE REDIRECT:

exports.remove = function(req, res) {
  var postId = req.params.id;
  Post.remove({ _id: postId }, function(err) {
    if (!err) {
            console.log('notification!');
            res.send(200);
    }
    else {
            console.log('error in the remove function');
            res.send(400);
    }
  });
};

remove gets called when an item (a post) is deleted. Everything works fine (I had to use res.send(200) to get it to not hang on the delete request) - but now I am having trouble redirecting. If I use res.redirect('/forum') inside the remove function, like this:

exports.remove = function(req, res) {
  var postId = req.params.id;
  Post.remove({ _id: postId }, function(err) {
    if (!err) {
            console.log('notification!');
            res.send(200);
    }
    else {
            console.log('error in the remove function');
            res.send(400);
    }
    res.redirect('/forum');
  });
};

It registers the redirect as a DELETE request that is trying to delete /forum, like this:

DELETE http://localhost:9000/forum 404 Not Found 4ms

All I am trying to do is refresh the page so that the list of posts is updated after the delete. Can anyone help?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ewizard
  • 2,801
  • 4
  • 52
  • 110
  • I have the same problem with you. Everything is fine. But `res.redirect` not reload the page. – Lin Du Nov 08 '16 at 02:28
  • @novaline - are you using angular too? I ended up having to do something on my frontend side, not the express side - see answer below. – ewizard Nov 08 '16 at 11:46
  • no, I use jQuery. I want to reload the page by server side. It's weird. The `res.redirect` work when I do the `create` operation (POST, form action). But the `delete` operation (POST, $.ajax) , the `res.redirect` is fine, with no error. But the problem is it's not refresh the page. I can see the network `response` returned from server side. – Lin Du Nov 09 '16 at 02:11
  • try using `window.location.href = url;` on your jquery side. that is straight javascript...i think the jquery version would be `$(window).location.href =` but both should do the same thing. `url` is where you want to be redirected to. you just need to put it in the right place. also with jquery . i can help u find the right place if you want. you can use it instead of server side res.redirect you just have to trigger it with a message from the server side. – ewizard Nov 09 '16 at 11:14
  • thanks. But my question is why browser do not refresh the page then I call `res.redirect` by server side. The http status is 200 and you can see the response(the `html` document) is returned by server side. I know how to do it by client side. Because of using `ajax`? Maybe the `form action` request will be ok? – Lin Du Nov 10 '16 at 01:49
  • yah, that was my question too, i was never able to figure out how to do it from server side. – ewizard Nov 13 '16 at 15:05

3 Answers3

7

I know this is late, but for anyone who sees this later, you can also manually reset the HTTP method to GET which should also work

exports.remove = function(req, res) {
  var postId = req.params.id;
  Post.remove({ _id: postId }, function(err) {
    if (!err) {
            console.log('notification!');
            res.send(200);
    }
    else {
            console.log('error in the remove function');
            res.send(400);
    }

    //Set HTTP method to GET
    req.method = 'GET'

    res.redirect('/forum');
  });
};
Calvin G
  • 71
  • 1
  • 1
6

@ewizard 's solution is great if you can fix this on the front end. However, if you want to fix this on the back end, you can add an optional Status Code argument to res.redirect like so:

res.redirect(303, "/forum");

This redirects for "Undefined Reason" which will default to a GET redirect.

See this SO post for more info.

ZebGir
  • 123
  • 1
  • 7
3

I got it working on my Angular side with $window.location.href = '/forum'; - just put it in the success function of the $http request that is part of the delete function that gets executed when the "Delete" button is clicked.

ewizard
  • 2,801
  • 4
  • 52
  • 110
  • And what if you need to delete something without being on a page first? – trysis Nov 03 '14 at 17:25
  • @trysis - is this a question? are you having a problem or trying to point out something I did wrong? It's been a while but for this problem I wouldn't need to delete the post without going to the page...and if i wanted to I could just remove it from the database more manually because I am admin... – ewizard Nov 03 '14 at 20:53
  • Oh, sorry. For most websites (including, I assume, yours) that's fine. It's just for some it wouldn't, and I was trying to make the question broader. Don't mind me. :) – trysis Nov 03 '14 at 22:43
  • I would like to do the redirect from the server side rather than having to send some object with instructions for the client side to redirect... anyone know of a way to do this? – JaKXz Jul 29 '15 at 23:25