Search code examples
angularangular-router

Angular URL mapping with routes for initial load


I have an angular project which has multiple routes say /category/fruit/apple so the full url is http://myserver/category/fruit/apple through router link everything is fine but if I open this link directly in typing in URL then it is 404 my backend is nodejs with express. And the 404 make sense as there is no route configured like this.

My Question, In this kind of situation how should I handle?

I thought I had, redirect to root http://myserver?path=category-fruit-apple and from the root component do the dynamic routing. Is it the correct way ?

Please suggest the best way.


Solution

  • There is a frontend-only solution (so, zero configuration server-side), too, namely hash routing. Add a line

    useHash: true
    

    in the root router module's configuration (then, the urls will look something like http://myserver/#/category/fruit/apple).