Node redirect not working on angular

Hi, I am new to node and I am creating a simple MEAN stack application to reduce the code that I am sending front files such as

app.use(express.static(path.join(__dirname, 'public')));

I also create a simple middleware for easy authentication

requireLogin = function (req, res, next) {
  if (!req.user) {
      console.log('redirecting :)');
      res.redirect('/');
  } else {
      next();
  }
};

app.use('/rooms',requireLogin);

I am trying to use this middleware on routes made in angular. but this does not work when I navigate my angular application (it works when I directly put the URL in the address bar). I also deleted / # /, which angular added.

    $locationProvider.html5Mode({
    enabled: true,
    requireBase: false
});

I use ui-router for routing.

+4
source share
1 answer

You should redirect to angular , but not to node.js application. For instance,

requireLogin = function (req, res, next) {
  if (!req.user) {
      console.log('User does not exist.');
      return false;
      // 
  } else {
      next();
  }
};

app.use('/rooms', requireLogin);

/rooms , .

- (-): , ( angular , )).

(angular ones): , - .

Express.js angular MEAN?.

+3

All Articles