简体   繁体   English

在 Express 中从一条路线重定向到另一条路线

[英]Redirect from one route to another in Express

I have following routes in nodejs我在nodejs中有以下路线

here is users.js route这是users.js路线

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这是 dashboard.js 路线

 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.我希望用户重定向到/dashboard/dashboard

How can I redirect users from one route to another in Express?如何在 Express 中将用户从一条路线重定向到另一条路线?

I had 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.我和你有同样的疑问,我来这里寻找答案但没有找到,所以我不得不自己在文档中进行一些挖掘以寻求解决方案,我找到了一个。

So coming to the answer now.所以现在来回答。 Using res.redirect('/dashboard') is taking to the path which is relative to the current route ie users/dashboard, if you have a separate route for dashboard then you'll have to use it this way: res.redirect('../dashboard/dashboard')使用res.redirect('/dashboard')是采用相对于当前路径的路径,即用户/仪表板,如果你有一个单独的仪表板路径,那么你必须以这种方式使用它: res.redirect( '../仪表板/仪表板')

Just for reference here is a preview of my project:仅供参考,这里是我的项目的预览:

routes folder路线文件夹

here is user_home.js route :这是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');  
}

PS: This is my first answer on stack overflow, glad that I could help someone out. PS:这是我关于堆栈溢出的第一个答案,很高兴我能帮助别人。

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.我在我的一条名为“contributions”的路线中有这个,当它被重定向时,它被呈现为“contributions/auth/login”,这是错误的。

The main thing i needed was 'localhost://port/auth/login.我需要的主要是'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, ie, 'contributions/auth/login' in my case.我将我的 ejs 锚标记更改为“/auth/login”,它将从根目录获取 url,以前是“auth/login”,它将 url 作为当前 url + 提供的 url,即,“contributions/auth/login”我的情况。

Hope this helps.希望这可以帮助。

Don't do response.redirect('/dashboard');不要做response.redirect('/dashboard'); with a / in it, as this will add /dashboard to the current url.里面有/ ,因为这会将/dashboard添加到当前 url。 Do this instead: response.redirect('dashboard');改为这样做: response.redirect('dashboard'); . .

in my case I've created a redirect in case I don't have a req.query parameter.在我的情况下,我创建了一个重定向,以防我没有 req.query 参数。 So after the redirect I added a return.所以在重定向之后我添加了一个返回。

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

Yup I am just 5 years late anyway, you are redirecting from a 'get' request to a 'post' request.是的,无论如何,我只是迟到了 5 年,您正在从“获取”请求重定向到“发布”请求。 That is why you are getting this error这就是您收到此错误的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM