Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 954
Pranoy Sarkar Avatar asked Sep 13 '19 06:09

Pranoy Sarkar


1 Answers

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).

like image 56
mbojko Avatar answered Sep 19 '22 15:09

mbojko