This content originally appeared on DEV Community and was authored by salim imuzai
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.
This content originally appeared on DEV Community and was authored by salim imuzai
salim imuzai | Sciencx (2021-04-12T08:54:26+00:00) How to redirect to a URL with Express JS. Retrieved from https://www.scien.cx/2021/04/12/how-to-redirect-to-a-url-with-express-js/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.