Om3ga
Om3ga

Reputation: 32853

Redirect from one route to another in Express

I have following routes in nodejs

here is users.js route

router.post('/users/login', function(request, response) {
    // let user login here and do all of the logic.

    // Once done then redirect user to dashboard in dashboard.js file
    response.redirect('/dashboard');
});

here is dashboard.js route

 router.get('/dashboard', function(request, response) {
     response.render('plans');
 });

Now when I login as a user when it tries to redirect me it says following.

cannot get Cannot GET /users/dashboard

I want users' to redirect to /dashboard/dashboard instead.

How can I redirect users from one route to another in Express?

Upvotes: 17

Views: 46933

Answers (5)

Akshay Parashar
Akshay Parashar

Reputation: 402

I had the same doubt as you had, I came here to look for the answer but found none so I had to do some digging in the docs myself for the solution, and I found one.

Using res.redirect('/dashboard') is taking to the path which is relative to the current route i.e users/dashboard, if you have a separate route for dashboard then you'll have to use it this way: res.redirect('../dashboard/dashboard')

Just for reference here is my project structure:

routes/
> index.js
> user_home.js
> users.js

Here are the contents of user_home.js:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.send("Hello User, welcome to user_home page");
});

router.get('/dashboard', function(req, res, next) {
  res.send("Hello User, this your personal dashboard");
});

module.exports = router;

and here is the code from the route from where i am redirecting :

else {
      console.log("Input validated!");
      res.redirect('../user_home/dashboard');  
}

Upvotes: 40

Chinmoy kr
Chinmoy kr

Reputation: 85

Yup I am just 5 years late anyway, you are redirecting from a 'get' request to a 'post' request. That is why you are getting this error

Upvotes: -2

waloar
waloar

Reputation: 108

in my case I've created a redirect in case I don't have a req.query parameter. So after the redirect I added a return.

       if(req.query.tk==null)
    {
        res.redirect('landing-page');
        return;
    }....

Upvotes: 2

Ninja Coder Tech
Ninja Coder Tech

Reputation: 41

Don't do response.redirect('/dashboard'); with a / in it, as this will add /dashboard to the current url. Do this instead: response.redirect('dashboard');.

Upvotes: 3

Mohammed Shoeb
Mohammed Shoeb

Reputation: 51

res.redirect('auth/login');

I had this in one of my route named 'contributions' and when it is redirected, it is rendered as 'contributions/auth/login' which is wrong.

The main thing i needed was 'localhost://port/auth/login.

i changed my ejs anchor tag to '/auth/login' which will take url from the root, It previously was 'auth/login' which took the url as current url + provided url, i.e., 'contributions/auth/login' in my case.

Hope this helps.

Upvotes: 1

Related Questions