代码之家  ›  专栏  ›  技术社区  ›  Nikita Vlasenko

在传递了参数的节点中找不到路径

  •  1
  • Nikita Vlasenko  · 技术社区  · 6 年前

    我正在努力 get 一页 express-router 当在里面的时候 url :id 作为参数提供,使用 passport 自定义回调和 网址 不知何故找不到。以下是我在主文件中的内容:

    var app = express();
    var index = require('./routes/index');
    
    app.post('/login', function(req, res, next) {
      passport.authenticate('local-signup', function(err, user, info) {
        console.log('passport callback');
        console.log(err);
        console.log(info);
        if (err) { return next(err); }
        if (!user) { return res.status(401).json(info); }
        req.logIn(user, function(err) {
          console.log('logIn function of /login path');
          if (err) { return next(err); }
          return res.redirect('/auth/' + user.local.username + '/');
        });
      })(req, res, next);
      console.log('end of login function');
    });
    app.use('/auth/:id/', passport.authenticate(['facebook-token', 'local-signup']), index);
    

    在里面 index 我有:

    router.get('/auth/:id/', function(req, res) {
      console.log("router of index.js is sending app.html");
      var appPath = path.join(__dirname, '..', 'public', 'app.html');
      res.sendFile(appPath);
    });
    

    我看到重定向到 /auth/nik1989/ 发生了,但是 网址 找不到。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Stamos    6 年前

    从阅读 Express 4.x API - Router

    // will handle any request that ends in /events
    // depends on where the router is "use()'d"
    router.get('/events', function(req, res, next) {
      // ..
    });
    
    // only requests to /calendar/* will be sent to our "router"
    app.use('/calendar', router);
    

    按照示例调用 events 你必须做的终点 /calendar/events .

    现在,在您的案例中,您清楚地注入中间件的方式 错误的

    router.get('/auth/:id/', function(req, res) {
      console.log("router of index.js is sending app.html");
      var appPath = path.join(__dirname, '..', 'public', 'app.html');
      res.sendFile(appPath);
    });
    
    app.use('/auth/:id/', passport.authenticate(['facebook-token', 'local-signup']), index);
    

    因为您必须调用的URL是 /auth/:id/auth/:id/ .

    如果你分解上面的代码,你所做的就是这样想

    app.use('/auth/:id/',passport..., router.get('/auth/:id/'...)
    

    有很多方法可以解决这个问题,我在下面举了一些例子。

    实例

    工作示例一

    router.get('/:id', function(req, res) {
      console.log("router of index.js is sending app.html");
      var appPath = path.join(__dirname, '..', 'public', 'app.html');
      res.sendFile(appPath);
    });
    
    app.use('/auth', passport.authenticate(['facebook-token', 'local-signup']), index);
    

    工作示例二

    router.get('/auth/:id', function(req, res) {
      console.log("router of index.js is sending app.html");
      var appPath = path.join(__dirname, '..', 'public', 'app.html');
      res.sendFile(appPath);
    });
    
    app.use(passport.authenticate(['facebook-token', 'local-signup']), index);
    
        2
  •  1
  •   elraphty    6 年前

    这应该是原因

    app.use('/auth/:id/', passport.authenticate(['facebook-token', 'local- 
    signup']), index);
    

    您的index.js文件中已经有一个具有相同URL结构的路由 /auth/:id/

    使用app.use(“/account”,accoutRouter)时; 这意味着他的应用程序调用的所有其他路由。使用中间件将 帐户/预先发送给它,例如帐户/登录、帐户/注册
    我在你的代码中没有看到护照初始化

    app.use(passport.initialize());
    app.use(passport.session());
    passport.use(new LocalStrategy(Account.authenticate()));
    passport.serializeUser(Account.serializeUser());
    passport.deserializeUser(Account.deserializeUser());
    

    但我相信使用

    router.get('/auth/:id/', function(req, res) {
      console.log("router of index.js is sending app.html");
      var appPath = path.join(__dirname, '..', 'public', 'app.html');
      res.sendFile(appPath);
    });
    

    app.use('/auth/:id/',passport.authenticate(['facebook-token','local-
    签署“]),索引);
    

    一定会导致错误