twitchax / AspNetCore.Proxy

ASP.NET Core Proxies made easy.
MIT License
524 stars 83 forks source link

Question: Proxying for CORS in Development #77

Open c17r opened 3 years ago

c17r commented 3 years ago

In prod, we're having HAProxy handle diverting any request beginning with /api/ to the backend server so everything is under the same origin and we don't have to worry about CORS.

For development we're using this library to handle the proxying in combination with spa.UseAngularCliServer(npmScript: "start"); Best I can figure, this is the simplest configuration to take anything request beginning with /api/, strip off /api/, and then proxying it to the backend:

app.UseProxies(proxies =>
{
    proxies.Map("api/{**rest}", 
        proxy => proxy.UseHttp((context, _) =>
        {
            var qs = context.Request.QueryString.Value;
            var url = context.Request.Path.ToString().Replace("/api/", "/");
            return $"https://localhost:5001{url}{qs}";
        }));
});

Is there another way built-in that I'm missing?

twitchax commented 3 years ago

This is the de facto best way to do this currently.

However, this does make me think that I could add a helper method to make it a little simpler.