• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

225
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 3 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 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error