【问题标题】:NodeJs + ExpressJs app routing odd behaviorNodeJs + ExpressJs 应用程序路由奇怪的行为
【发布时间】:2015-09-04 17:42:38
【问题描述】:

我正在学习 ExpressJS。到目前为止,我已经设置了一个简单的 todo 应用程序,使用 PassportJS 进行用户身份验证。我使用 Mongoose 作为存储库。网络上没有任何东西可以解释我在路由设置中看到的奇怪行为。

场景:

  • 当我点击 get /passport 时,它会直接转到护照页面 (登录/注册)
  • 当我点击 get /aslkdjf 时,如果 用户未登录,否则将定向到文件
    /public/index.html)
  • 当我点击 get / 它应该直接到护照页面,如果用户 未登录,但它转到 /public/index.html 并且 我的 todo 应用程序将失败,因为 /api/todos 下的 req.user.username 是 无敌

奇怪的是,当我删除 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/

【问题讨论】:

    标签: angularjs node.js express mongoose passport.js


    【解决方案1】:

    显然,问题在于 HTML 默认会在根目录中的任何文件夹中查找 index.html 文件。当我将 html 文件更改为 abc.html 之类的其他文件时,问题就解决了。对我来说似乎是一个错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-23
      • 2017-09-01
      • 2016-09-25
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多