Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 646
Om3ga Avatar asked Feb 03 '17 23:02

Om3ga


1 Answers

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');.

like image 175
Ninja Coder Tech Avatar answered Oct 21 '22 03:10

Ninja Coder Tech