How to redirect to a URL with Express JS

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.


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.

Example:
Alt Text

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.

Alt Text

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.

Alt Text


This content originally appeared on DEV Community and was authored by salim imuzai


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to redirect to a URL with Express JS." salim imuzai | Sciencx - Monday April 12, 2021, https://www.scien.cx/2021/04/12/how-to-redirect-to-a-url-with-express-js/
HARVARD
salim imuzai | Sciencx Monday April 12, 2021 » How to redirect to a URL with Express JS., viewed ,<https://www.scien.cx/2021/04/12/how-to-redirect-to-a-url-with-express-js/>
VANCOUVER
salim imuzai | Sciencx - » How to redirect to a URL with Express JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/12/how-to-redirect-to-a-url-with-express-js/
CHICAGO
" » How to redirect to a URL with Express JS." salim imuzai | Sciencx - Accessed . https://www.scien.cx/2021/04/12/how-to-redirect-to-a-url-with-express-js/
IEEE
" » How to redirect to a URL with Express JS." salim imuzai | Sciencx [Online]. Available: https://www.scien.cx/2021/04/12/how-to-redirect-to-a-url-with-express-js/. [Accessed: ]
rf:citation
» How to redirect to a URL with Express JS | salim imuzai | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.