Angular 2 deployed to heroku, routes only work if i navigate to root page first

5 views
Skip to first unread message

Stephan Brown

unread,
Apr 24, 2017, 10:35:07 PM4/24/17
to Angular and AngularJS discussion
Hi everyone,

I have an Angular 2 application deployed to heroku. I was able to get to deploy but then there's a weird issue. I'm unable to navigate to any of the routes of the application until i navigate to root first. Anyone else have had this issue before?

Sander Elias

unread,
Apr 24, 2017, 10:44:39 PM4/24/17
to Angular and AngularJS discussion
Hi Stephan,


You need to tweak your Heroku's settings to return your index.html instead of a 404. If you use node with express as a server add something like this:

const path = require('path');
// For all get requests, send back index.html instead of 404
app
.get('/*', function(req, res) {
   res
.sendFile(path.join(__dirname + '/dist/index.html'));
});

Hope this helps you a bit,
Regards
Sander



Reply all
Reply to author
Forward
0 new messages