Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect page after post request [Express 4]

/**
 * @api {post} /logout Logout from system
 * @apiName Logout
 * @apiGroup Login
 */
router.post("/logout", function (req, res) {
  req.logout();
  req.session.destroy();
  return res.redirect("/");
});

I've read Node.js Express : How to redirect page after processing post request? but could not figure out the answer.

I recently changed logout to POST instead of GET. After doing so, redirect does'nt work

POST /logout 307 4.912 ms - 36
POST / 302 3.922 ms - 23
GET / 200 7.519 ms - -

I can manually do it on client side, but I want to know how to do it on server side as well. Is this possible?

CLIENT

HTML

<a href="javascript:;" onclick="logOut();">

JS

 function logOut() {
        $.post("/logout");
    }
like image 748
Zanko Avatar asked Jul 19 '16 04:07

Zanko


People also ask

How do I redirect a page in express?

The res. redirect() function lets you redirect the user to a different URL by sending an HTTP response with status 302. The HTTP client (browser, Axios, etc.) will then "follow" the redirect and send an HTTP request to the new URL as shown below.

CAN POST request be redirected?

in response to a POST request. Rather, the RFC simply states that the browser should alert the user and present an option to proceed or to cancel without reposting data to the new location. Unless you write complex server code, you can't force POST redirection and preserve posted data.

How do I redirect a response in node js?

redirect() function, we can now discuss how to redirect back to original URL in NodeJS. Back redirect: We can use this method to redirects the request back to the referrer. If no referrer is present, the request is redirected to “/” route by default.

Can't set headers after they are sent?

The "Cannot set headers after they are sent to the client" error occurs when the server in an express. js application sends more than one response for a single request, e.g. calling res. json() twice. To solve the error, make sure to only send a single response for each request.


1 Answers

There are no redirects from a Javascript generated Ajax call which your $.post() is. Ajax sends a request, gets a response. An ajax call by itself does not change the page location at all. That's a characteristic of Ajax calls.

Redirects work when the browser is loading a new page and the server tells it to change what page it is loading to a new source, not when the browser just sends an Ajax call.

You can, of course, use your client-side Javascript to decide to redirect from the client side after your $.post() finishes. You could even have the response from the $.post() be the new location and your client-side Javascript could then just set window.location to that new URL.

function logOut() {
    $.post("/logout").then(function(data) {
        window.location = data.redirectUrl;
    });
}

And, on the server:

router.post("/logout", function (req, res) {
  req.logout();
  req.session.destroy();
  res.send({err: 0, redirectUrl: "/"});
});
like image 173
jfriend00 Avatar answered Oct 04 '22 01:10

jfriend00