I have an angular2 application in heroku, and I'm having trouble with the router. In localhost everything works like a charm, but when I deploy to heroku and try to access by any route that is not index i got 404 error, if I go index, then navigate trough page the routing occurs normally, unless I reload the page, then i get another 404, here's the piece of my package.json used by heroku
"heroku-prebuild": "npm install http-server -g",
"heroku-postbuild": "ng build --target=production --environment=prod && rsync -a dist/* .",
"start": "http-server dist/",
Do I need to setup any express rewriting to be used in my Procfile?
Asked
Active
Viewed 920 times
5

Matthew Green
- 10,161
- 4
- 36
- 54

Wendel Nascimento
- 191
- 6
-
Just a suggestion - search HTML5 routing in https://m.alphasights.com/using-nginx-on-heroku-to-serve-single-page-apps-and-avoid-cors-5d013b171a45#.1yk8sb720 – Günter Zöchbauer Nov 08 '16 at 18:09
-
You could also add `{provide: LocationStrategy, useClass: HashLocationStrategy}` to providers of your `AppModule`. See also http://stackoverflow.com/questions/36861628/location-and-hashlocationstrategy-stopped-working-in-beta-16 – Günter Zöchbauer Nov 08 '16 at 18:21
-
@GünterZöchbauer Without `HashLocationStrategy` the only way to achieve that is using NGINX? – Wendel Nascimento Nov 08 '16 at 20:26
-
I just googled for Heroku and HTML5 routing. I don't know anything about Heroku. – Günter Zöchbauer Nov 08 '16 at 20:27
-
Hello, have you found anything now? I have the same issue :) – Adrien Feb 03 '17 at 18:16
-
@Adrien No. I had to change the history to use hash. It wasn't the best solution but it was the only that worked. I feel your pain :( – Wendel Nascimento Feb 06 '17 at 10:02
1 Answers
1
It seems to be a problem from the server , angular knows routing but your server doesnot know all these paths. The simple solution is to redirect all the paths to your main index.html . Like this,
app.get('*', function (req, res) {
res.sendfile('./dist/index.html'); // load our index.html file
});
This will not give any 404 error , all your paths will be redirected to main path i.e index.html and angular routing will run the same as it was in your local host .

Daniyal Javaid
- 1,426
- 2
- 18
- 32
-
2I still got the issue . I applied this already . And it is only refresh page on a page not root that is showing internal error – Mustafa Oct 28 '17 at 10:03