1

I have some routes defined in my App component:

@routeConfig([
{ 
    path:'login', 
    name: 'Login', 
    component: Login
}}

And a very basic node express loader:

var express = require('express');
var app = express();

app.use(express.static('app'));
app.use('/node_modules', express.static(__dirname + '/node_modules/'));

app.get('/home', function(req, res, next) {
  next();
});

app.listen(3000, function () {
  console.log('Serverlistening on port 3000!');
});

My issue is that going to '/login'/ is intercepted by node express and it says 'cannot get /login'. I want Angular2 to handle it. It worked fine in node lite server but not in express.

How can I configure express to ignore these routes and allow Angular to handle them?

williamsandonz
  • 15,864
  • 23
  • 100
  • 186

1 Answers1

2

You could try to configure your route this way:

@routeConfig([
  { 
    path:'/login', // <---------- 
    name: 'Login', 
    component: Login
  }
])

Edit

If you hit the request directly, you need to have something to return the content of the index.html file for the request into your Express application.

This must be done for each route defined in Angular2. Otherwise you will have 404 errors.

See this question for more details:

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Nop that's not working. I'm hitting the URL directly from the browser (not clicking on any links). The issue is that Express get's the request first, then can't find a match so throws an error. – williamsandonz Apr 27 '16 at 15:14
  • 1
    If you hit the request directly, you need to have something to return the content of the `index.html` file for the request into your Express application... – Thierry Templier Apr 27 '16 at 15:15
  • See this question: http://stackoverflow.com/questions/35284988/angular-2-404-error-occur-when-i-refresh-through-browser/35285068#35285068 – Thierry Templier Apr 27 '16 at 15:17
  • yes thanks I actually just figured this out as well lol :-/ Do you want to post an answer, explaining that all that is needed, is a catch all * route in node express and to serve the index.html? – williamsandonz Apr 27 '16 at 15:20