Member-only story
How to redirect user’s browser URL to a different page in Nodejs?
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.