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, superAgent, etc.) will then "follow" the redirect and send an HTTP request to the new URL as shown below.
The res.redirect() function also lets you specify an HTTP status other than 302. The 302 status is considered a temporary redirect, which means search engines will still crawl the existing URL. If you want to indicate the URL has permanently changed, you should send a response with HTTP status 301.
Handling POST Requests
There's some nuances about which status code you should use for POST requests. Strictly speaking, HTTP 301 and 302 are not required to keep the same method and body content when redirecting. If you're redirecting a POST request, you should use HTTP 307 as a replacement for HTTP 302, and HTTP 308 as a replacement for HTTP 301.
Top comments (0)