Stop Angular 2 from overriding express routing

98 Views Asked by At

I'm totally stumped as to why this is happening and can't seem to find the answer anywhere. Basically, my '/' request is never getting to my node.js server (using express) and is automatically sending my index.html file. When I created my Angular 2 project, I specifically turned of routing since I didn't need it. So routes shouldn't be seen by angular at all, yes?

routes.js

app.get('/', function(req, res) {
    console.log("to index: " + req.user);
    if(req.user){
        res.sendfile("../../dist/WebAppAngular/index.html");
    } else {
        res.redirect('/login');
    }
});
app.get('/login', function(req, res) {
    res.sendfile("../../dist/WebAppAngular/login.html");
});

index.html

<!doctype html>
<link rel="stylesheet" href="static/css/bootstrap.min.css">
<script src="static/js/jquery-3.3.1.min.js"></script>
<script src="static/js/popper.min.js"></script>
<script src="static/js/bootstrap.min.js"></script>
<script src="static/js/Chart.min.js"></script>
<script src="static/js/all.js"></script>
<link rel="stylesheet" href="static/css/flags.css">
<link rel="stylesheet" href="static/css/all.css">
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>WebAppAngular</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>

When I make a request to '/', it never gets to the print statement and just sends the index page. Even when I comment out everything under app.get('/'...) it still sends the login page. So Angular is the one sending it, not the server. I looked at my requests using Fiddler and '/' returns '200 OK' and does not redirect to '/login'. However, if I go directly to '/login' the request gets through to express.

I'm new to Angular and I can't find any reason why this would be happening. I'm not using Angular's routing module at all. I tried getting rid of <base href="/"> and also changing the href to something else, and neither did anything.

Thanks anyone for any help! I can edit with more of my files if needed. I am so confused, please help.

0

There are 0 best solutions below