Author -  Sai gowtham

How to redirect to another page in Node.js

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

Note: This tutorial assumes that you are using express framework in node.js.

In node.js, we can use the res.redirect() method to redirect a user to the another page.

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 ‘/login’ page to ‘/dashboard’ page.

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

By default, Node.js 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 website instead of same site, you need to pass a full url of the site like this.

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

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

Here is an example:

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