angular-cli: routing doesnt work after deployment in angular2
angular-cli: 1.0.0-beta.18 node: 6.4.0 os: win32 x64 Windows 10
I have created an angular2 app & deployed it successfully.
app starts correctly after taking some time to load & then I am able to navigate to different components.
but when I put direct address in URL as :
www.sample.com/about
it says 404 error.
even when I am creating bundle using ng build --prod
, it creates dist
folder correctly & I am able to navigate to different components using links only.
but I cant access components directly from the URL in my local system also.
I am using http-server
.
are there any extra configurations required to make it work?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 29 (5 by maintainers)
Hi,
Yes - you do 😃 Angular CLI does not automatically add routing to the app just yet therefore you would need to modify
app.module.ts
file as example below:If this issue occurred in production, follow below steps
ng build --prod
This will create a dist folder. The files inside dist folder are ready for deployment by any mode.@naveenkumarkg19 see https://angular.io/guide/deployment for information about deployment. Likely you need to configure index fallback (https://angular.io/guide/deployment#routed-apps-must-fallback-to-indexhtml).
http-server
doesn’t support fallback to index.html, thus you can’t use HTML5 push navigation with it. You can use https://github.com/johnpapa/lite-server instead, which supports fallback.@irthos you can change the router to use A hash location strategy instead of the history API and that will work on gh pages.
Apache: add a rewrite rule to the .htaccess file with following content
@Bhushan001 U have to add something like that in ur
server.js
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/index.html'));
});
As @deebloo mentioned, you need to properly configure your server or switch to angular’s hash location strategy.
you need to setup something to redirect to your index.html page or change the default location strategy to hash.
This work for me !!
You need to configure this on the server, direct access e.g. www.yourdomain.eu/home will look for “home” directory which does not exist… All requests need to go through index.html so on apache server all you need is .htaccess from this https://angular.io/guide/deployment
I have the same issue… “ng serve” works great and changes the url according to route configuration. “ng build --prod” changes the url according to route configuration but they cannot be navigated again
+1 Then pushing to gh-pages is probably not much good since we can’t update their servers for proper html5mode. Same as with ng1.x, no?