expressjs.com icon indicating copy to clipboard operation
expressjs.com copied to clipboard

res.redirect: Code examples should show default HTTP status code 302 instead of 301

Open zionsg opened this issue 6 months ago • 2 comments

Description

Was migrating from Express 4 to 5 recently. Followed the docs and replaced res.redirect(url) with res.redirect(301, url) as per the code examples in the migration guide.

My bad on not checking that 301 is a permanent redirect. Only found out later when I could not log out from my application.

Read the fine text and saw that the status code, if not specified, defaults to 302, which is a temporary redirect. Updated my application code but had to clear browser cache.

Expectations

Code examples for res.direct(status, url) should use the default status code, which is 302.

  • https://expressjs.com/en/5x/api.html#res.redirect
  • https://expressjs.com/en/guide/migrating-5.html#res.redirect
  • https://expressjs.com/en/4x/api.html#res.redirect

Code examples should use default values to prevent confusion, as developers would likely jump straight to them instead of reading through paragraphs of text, especially when the method parameters are few and seemingly straightforward.

zionsg avatar May 13 '26 01:05 zionsg

Opened expressjs/express#7236.

moaedy avatar May 13 '26 07:05 moaedy

The migration guide says to replace res.redirect(url, 301) with res.redirect(301, url). Nowhere does it say that res.redirect(url) is not supported. It uses 301, because it is the status that developers are most likely to use if they don't want the default 302.

I think it would be more obvious, if the signatures in migration guide showed that status is optional.

krzysdz avatar May 13 '26 08:05 krzysdz