2015-06-18 27 views
-1

我正在學習ExpressJS。到目前爲止,我已經使用PassportJS設置了一個使用用戶身份驗證的簡單待辦事項應用程序。我使用Mongoose來存儲庫。網絡上沒有任何東西可以解釋我在路由設置中看到的奇怪行爲。NodeJs + ExpressJs應用程序路由奇數行爲

場景:

  • 當我擊得/護照就會直接到護照頁 (登錄/註冊)
  • 當我擊得/ aslkdjf它將如果 用戶定向到護照頁沒有登錄,否則將直接將文件
    /public/index.html)
  • 當我擊得/應該直接到護照頁面,如果用戶 沒有登錄,但它去/公共/index.html而不是 我的待辦事項應用程序將失敗,因爲在/ API req.user.username /待辦事項是 undefiend

奇怪的是,當我刪除router.get( '/ *',...的配置,我的應用程序仍然會轉到public/index.html,當我點擊基本路徑'/'時,但不是當我點擊'/ asdfa'時。

... 
 
    
 
    function loggedIn(req, res, next) { 
 
     if (req.user) { 
 
      next(); 
 
     } else { 
 
      res.redirect('/passport'); 
 
     } 
 
    } 
 

 
    var router = express.Router(); 
 
// passport ---------------------------------------------------------------- 
 
// get passport page 
 
router.get('/passport', notLoggedIn, function(req, res) { 
 
    res.sendfile('./public/passport.html'); 
 
}); 
 

 
// post login 
 
router.post('/login', passport.authenticate('login', { 
 
    successRedirect: '/', 
 
    failureRedirect: '/passport', 
 
    failureFlash: true 
 
})); 
 

 
// post registration 
 
router.post('/signup', passport.authenticate('signup', { 
 
    successRedirect: '/', 
 
    failureRedirect: '/passport', 
 
    failureFlash: true 
 
})); 
 

 
router.get('/logout', function(req, res) { 
 
    req.session.destroy(); 
 
    req.logout(); 
 
    res.redirect('/'); 
 
}); 
 

 
// api --------------------------------------------------------------------- 
 
// get all todos 
 
router.get('/api/todos', function(req, res) { 
 

 
    // use mongoose to get all todos in the database 
 
    Todo.find({owner: req.user.username}, function(err, todos) { 
 

 
     // if there is an error retrieving, send the error. nothing after res.send(err) will execute 
 
     if (err) 
 
      res.send(err) 
 

 
     res.json(todos); // return all todos in JSON format 
 
    }); 
 
}); 
 

 
// create todo and send back all todos after creation 
 
router.post('/api/todos', function(req, res) { 
 

 
    // create a todo, information comes from AJAX request from Angular 
 
    Todo.create({ 
 
     owner: req.user.username, 
 
     text : req.body.text, 
 
     done : false 
 
    }, function(err, todo) { 
 
     if (err) 
 
      res.send(err); 
 

 
     // get and return all the todos after you create another 
 
     Todo.find({owner: req.user.username}, function(err, todos) { 
 
      if (err) 
 
       res.send(err) 
 
      res.json(todos); 
 
     }); 
 
    }); 
 

 
}); 
 

 
// delete a todo 
 
router.delete('/api/todos/:todo_id', function(req, res) { 
 
    Todo.remove({ 
 
     _id : req.params.todo_id 
 
    }, function(err, todo) { 
 
     if (err) 
 
      res.send(err); 
 

 
     // get and return all the todos after you create another 
 
     Todo.find({owner: req.user.username}, function(err, todos) { 
 
      if (err) 
 
       res.send(err) 
 
      res.json(todos); 
 
     }); 
 
    }); 
 
}); 
 

 
// application ------------------------------------------------------------- 
 
router.all('*', loggedIn); 
 
router.get('/*', function(req, res) { 
 
    res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end) 
 
}); 
 

 
    app.use('/', router); 
 
    app.listen(3000); 
 
    console.log("App listening on port 3000");

有人能向我解釋是怎麼回事?我想要實現的是讓應用程序將用戶重新路由到登錄頁面,當他們沒有登錄時,他們去www.myapp.com/

回答

0

所以顯然問題是HTML默認找到索引.html文件放在根目錄下的任何文件夾中。當我將html文件更改爲其他類似abc.html的問題時,問題就解決了。對我來說就像一個錯誤。