0

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.

lilibug1
  • 31
  • 5
  • Perhaps a silly question, but how are you serving your application? Are you running your Node application, or are you running Angular via: `ng serve` ? – Brandon Taylor Nov 19 '18 at 19:41
  • Running using node. Building using ng build --watch. – lilibug1 Nov 20 '18 at 15:13
  • How are you making the request to the root path? Postman? A browser, by entering localhost:port into the address bar? – Brandon Taylor Nov 20 '18 at 15:16
  • A browser by using localhost:port. From what I've read the browser should make the request right to the server (and Angular should have nothing to do with it) but it doesn't look like that's what happens – lilibug1 Nov 20 '18 at 16:14
  • The browser won't route any requests through Angular. If your Angular app is being served at all, the request to / *has* to be going through Express. There's no other way it could work. – Brandon Taylor Nov 20 '18 at 17:11

0 Answers0