angularjs - NodeJs + ExpressJs app routing odd behavior -


i learning expressjs. far have setup simple todo app user authentication using passportjs. use mongoose repository. there nothing in web explain odd behavior i'm seeing route setup.

scenario:

  • when hit /passport direct passport page (login/signup)
  • when hit /aslkdjf direct passport page if user not logged in, else direct file
    /public/index.html)
  • when hit / should direct passport page if user not logged in, goes /public/index.html instead , todo app fail req.user.username under /api/todos undefiend

strangely, when remove router.get('/*', ... configuration, app still go public/index.html, when hit base path '/', not when hit '/asdfa'.

    ...            function loggedin(req, res, next) {          if (req.user) {              next();          } else {              res.redirect('/passport');          }      }        var router = express.router();  // passport ----------------------------------------------------------------  // 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 ---------------------------------------------------------------------  // todos  router.get('/api/todos', function(req, res) {        // use mongoose todos in database      todo.find({owner: req.user.username}, function(err, todos) {            // if there error retrieving, send error. nothing after res.send(err) execute          if (err)              res.send(err)            res.json(todos); // return todos in json format      });  });    // create todo , send todos after creation  router.post('/api/todos', function(req, res) {        // create todo, information comes ajax request angular      todo.create({          owner: req.user.username,          text : req.body.text,          done : false      }, function(err, todo) {          if (err)              res.send(err);            // , return todos after create          todo.find({owner: req.user.username}, function(err, todos) {              if (err)                  res.send(err)              res.json(todos);          });      });    });    // delete 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);            // , return todos after create          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 single view file (angular handle page changes on front-end)  });        app.use('/', router);      app.listen(3000);      console.log("app listening on port 3000");

can explain me going on? want achieve make app re-route user login page when not logged in , go www.myapp.com/

so apparently problem html defaults finding index.html file in folder within root. when change html file else abc.html problem solved. seems bug me.


Comments

Popular posts from this blog

python - How to create jsonb index using GIN on SQLAlchemy? -

PHP DOM loadHTML() method unusual warning -

c# - TransactionScope not rolling back although no complete() is called -