Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Nodejs redirect from POST method not working

I have a logout button, which sends a POST request. After that it should redirect the user to the main page, but nothing happens. I also tried to set the status code to 302, 303...

Code:

//on click this is executed
function logout() {
  fetch('/logout', { method: 'POST' });
}
//this is called on POST request
const logout = (_req, res) => {
  res.clearCookie('token');
  res.locals.payload = undefined;
  res.redirect('/');                    //problem
};
//logged requests
POST /logout 302 23 - 0.865 ms
GET / 304 - - 6.058 ms
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If your /logout endpoint accepts GET requests, try redirecting the user there instead. For example:

function logout() {
    document.location = '/logout';
}

Otherwise, add a redirect in JavaScript once the cookies are cleared:

async function logout() {
  await fetch('/logout', { method: 'POST' });
  document.location = '/';
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!