Does Angular Have an Equivalent to C#'s "UsePathBase"

Viewed 39

We have an angular application behind an AWS ALB. The load balancer directs traffic to the angular app when requests are made to a certain route example.com/balancer-route/.... But this then means that requests hit the angular app with the additional balancer-route in the URI. We need these to be ignored so files are served correctly.

Is there an equivalent to app.UseBasePath in C#? In C# this would essential ignore the /balancer-route part of the requests and serve the endpoints as normal. It would also continue to serve requests when the base path is not present.

We're aware this can be solved using NGINX but would prefer an application based solution where the path could be passed to the application at runtime.

1 Answers

By using the redirect capabilities of Angular Routing you can get what you are looking for. Take this routes for example:

    const routes: Routes = [
       { path: '', component: HomeComponent },
       { path: 'quotes', component: QuoteListComponent },
       { path: 'quote/:id', component: QuoteComponent }
    ];

you can surf to http://some.com/, http://some.com/quotes and http://some.com/quote/1,

and, you want to redirect to traffic from http://some.com/balancer-route, http://some.com/balancer-route/quotes and http://some.com/balancer-route/quote/1 to the first routes examples respectively.

Therefore we will group all our Routes into one object with children routes, and we will add another Route with reidrect to the same object that groups all the routes

    const routes: Routes = [
       {
          path: '', children: [
             { path: '', component: HomeComponent },
             { path: 'quotes', component: QuoteListComponent },
             { path: 'quote/:id', component: QuoteComponent }
          ]
       }, {
          path: 'balancer-route', redirectTo: ''
       }
    ];
Related