Node.js Express redirect example
Posted on: October 19, 2021 by Deven
HTTP Redirects are used to send users’ browsers to another page instead of the current one. There are 2 types of redirects, a 302 redirect which is considered temporary, and a 301 redirect which is considered permanent. The only difference between them is that the browser can cache and remember the 302 redirects so that the original request need not be made again.
Node.js Express redirect example
app.get('/old', (req, res) => {
res.redirect('/new');
});
app.get('/new', (req, res) => {
res.send('Redirected to new route');
});
Redirects can be relative to the root of the host name or can be relative to the current URL.It can also be a fully-qualified URL for redirecting to a different site:
res.redirect('http://www.google.com')
A back redirection redirects the request back.
res.redirect('back')
Share on social media
//