Author -  Sai gowtham

How to redirect from one route to another in Express

In this tutorial, we will learn about how to redirect a user from one route to another route in the express app.

In express, we can use the res.redirect() method to redirect a user to the different route.

The res.redirect() method takes the path as an argument and redirects the user to that specified path.

In this example, we are redirecting a user from /lab to /dashboard.

app.get('/lab',(req,res)=>{
   res.redirect('/dashboard');
})

By default, express creates a 302 redirect.

If you want to create a permanent redirect, you need to specify a status code 301 like this.

app.get('/lab',(req,res)=>{
   res.redirect(301, '/dashboard');
})

If you are redirecting a user to a different site, you need to pass a full url of the site like this.

app.get('/lab',(req,res)=>{
   res.redirect(301, 'https://google.com/');
})

Path relative redirects are also possible in express, for example if you are on http://localhost:3000/users/sai/edit the following .. will redirect to http://localhost:3000/users/sai.

res.redirect('..')

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY