Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

res.redirect() runs in Express but the browser does not go there

Errorres.redirect() is called, the server log shows no error, and the page stays where it was

Short answer

res.redirect() only sends a status code and a Location header; the client decides what to do with it. It looks broken when the request came from fetch (the script follows it, the page stays), when the browser cached a 301, when a POST turns into a GET, when a relative path resolves elsewhere, or when an Express 4 call form was removed in 5.

  • Applies to Express 5, Express 4.21, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the server directly, without a browser, what it sends. The status line and the Location header show whether the server side is right; if they are, the cause is on the client side.

    curl -i http://localhost:3000/old-path
  2. 2Match what you see, then jump to its fix

    1. 1In the Network tab the target address is requested, but the page does not move; response.redirected is trueThe request was made by fetch in browser JavaScriptFix
    2. 2curl shows the new Location, but the browser keeps going to the old destinationThe browser cached a 301 redirectFix
    3. 3The route you redirect to is hit with GET, and there is no bodyA redirected POST arrives as a GETFix
    4. 4The Location value has no leading slash, and the browser ends up in a different directory than the route's ownA relative path resolves somewhere you did not expectFix
    5. 5The problem started with the move to Express 5, in calls that use 'back' or put the status after the URLAn Express 4 call form that Express 5 removedFix

What does the error look like?

  • res.redirect() is called, the server log shows no error, and the page stays where it was
  • The browser still goes to the old address after the redirect target was changed in code
  • The target route receives a GET request with no body after a POST was redirected
  • res.redirect('back') no longer returns to the previous page after upgrading to Express 5

Cause 1 of 5

The request was made by fetch in browser JavaScript

1Check

Find the code that sends the request. With fetch the redirect mode is follow unless you change it, so the script follows the redirect itself and receives the final response. The address in the tab does not change, because nothing navigated.

const res = await fetch('/login', { method: 'POST', body: data });
console.log(res.redirected, res.url);

2Fix

Let the script do the navigation. Make the route answer a script request with the target address as JSON, and set window.location from it. Keep res.redirect() for requests the browser makes itself, such as a plain form post or a link.

// Express: answer the script, not the browser
app.post('/login', (req, res) => {
  res.json({ next: '/dashboard' });
});

// Browser
const res = await fetch('/login', { method: 'POST', body: data });
const { next } = await res.json();
window.location = next;

3Verify

After the request finishes, the address bar shows the new path.

console.log(window.location.pathname);

Confirmed by fetch.spec.whatwg.org

Did this fix it?

Cause 2 of 5

The browser cached a 301 redirect

1Check

RFC 9110 says a 301 response is heuristically cacheable, so a browser may reuse it without asking your server again. Check which status your code sends: the number is the first argument of res.redirect().

curl -i http://localhost:3000/old-path
grep -rn "redirect(301" --include="*.js" --exclude-dir=node_modules .

2Fix

Use the default 302 (or 307) while the destination can still change, and keep 301 or 308 for moves that are final. A browser that already cached the old 301 may not ask your server again, so test with another browser or a fresh profile.

res.redirect('/new-path');            // 302 Found, the default
// only when the move is permanent:
// res.redirect(301, '/new-path');

3Verify

The first line of the response is a 302 and the Location header holds the new path.

curl -sI http://localhost:3000/old-path | head -n 2

Confirmed by rfc-editor.org

Did this fix it?

Cause 3 of 5

A redirected POST arrives as a GET

1Check

RFC 9110 allows a client to change POST to GET after a 301 or 302 for historical reasons; 307 forbids changing the method, and 303 is the code meant for sending the client to a page to view after a POST. Log the method in the target route.

app.all('/thanks', (req, res, next) => {
  console.log(req.method);
  next();
});

2Fix

Choose the status for what you mean. After a form submit that should end on a result page, use 303 and a GET route for that page. To send the same method and body on to another address, use 307.

// show a result page after a POST
res.redirect(303, '/thanks');

// repeat the same POST at another address
res.redirect(307, '/api/v2/orders');

3Verify

The first response is a 303 (or 307) with the expected Location.

curl -i -X POST http://localhost:3000/submit -d 'a=1'

Confirmed by rfc-editor.org

Did this fix it?

Cause 4 of 5

A relative path resolves somewhere you did not expect

1Check

Express resolves a relative redirect against the current URL, and a trailing slash on the current URL changes the result: from /blog/admin/ the path post/new leads to /blog/admin/post/new, but from /blog/admin it leads to /blog/post/new. Search for redirects without a leading slash.

grep -rn "res.redirect(['\"][^/h]" --include="*.js" --exclude-dir=node_modules .

2Fix

For a fixed destination, start the path with a slash so it is relative to the host root, or give a full URL. Keep the relative forms for the cases where you really mean from here.

res.redirect('/admin/post/new');

3Verify

The Location header holds the root-relative path.

curl -sI http://localhost:3000/blog/admin | grep -i '^location'

Confirmed by expressjs.com

Did this fix it?

Cause 5 of 5

An Express 4 call form that Express 5 removed

1Check

The Express 5 migration guide removes two forms: the magic string back (res.redirect('back')) and the signature res.redirect(url, status). Search for both.

grep -rnE "redirect\(['\"]back['\"]" --include="*.js" --exclude-dir=node_modules .
grep -rnE "redirect\([^,)]+, *[0-9]{3}\)" --include="*.js" --exclude-dir=node_modules .

2Fix

Put the status first, and replace back with the referrer, falling back to a fixed page.

// old: res.redirect('/login', 301);
res.redirect(301, '/login');

// old: res.redirect('back');
res.redirect(req.get('Referrer') || '/');

3Verify

Called without a referrer, the route now redirects to the fallback.

curl -sI http://localhost:3000/go-back | grep -i '^location'

Confirmed by expressjs.com

Did this fix it?

How do I stop it coming back?

  • Pick the redirect status on purpose: 302 by default, 303 after a form POST, 307 or 308 to keep the method, and 301 only for moves that are final.
  • Answer requests made by scripts with JSON, and keep res.redirect() for navigations the browser makes by itself.
  • Start paths for fixed destinations with a slash, so a trailing slash in the current URL cannot change them.
  • When you upgrade to Express 5, search the code for the removed call forms before you deploy.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Express crashes with ERR_HTTP_HEADERS_SENT

Error: Can't set headers after they are sent to the client

This error means your code tried to write headers or a second response after Express already sent one.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.