Member-only story

How to redirect user’s browser URL to a different page in Nodejs?

Ckmobile
2 min readJan 9, 2021

--

In previous article, we already learnt how to do routing.

In this article, we are going to demonstrate how to redirect the “/contact-us” to “/contact”.

Full Course

First, we need to add another cases

case '/contact-us':res.statusCode = 301;res.setHeader('Location', '/contact')res.end();break;

We do not need to append the path because we are just going to redirect. We need to change the status code to 301, which mean the resource requested has been definitively moved to the URL given by the Location headers.

--

--

Ckmobile
Ckmobile

Written by Ckmobile

Teaching JavasScript, React, React Native, MongoDB and NodeJS https://linktr.ee/ckmobile

No responses yet