private static void UseProxyToLocalParcelDevMiddleware(this IApplicationBuilder appBuilder, string publicPath, int proxyToPort, TimeSpan requestTimeout) { // Note that this is hardcoded to make requests to "localhost" regardless of the hostname of the // server as far as the client is concerned. This is because ConditionalProxyMiddlewareOptions is // the one making the internal HTTP requests, and it's going to be to some port on this machine // because aspnet-webpack hosts the dev server there. We can't use the hostname that the client // sees, because that could be anything (e.g., some upstream load balancer) and we might not be // able to make outbound requests to it from here. // Also note that the webpack HMR service always uses HTTP, even if your app server uses HTTPS, // because the HMR service has no need for HTTPS (the client doesn't see it directly - all traffic // to it is proxied), and the HMR service couldn't use HTTPS anyway (in general it wouldn't have // the necessary certificate). var proxyOptions = new ConditionalProxyMiddlewareOptions("http", "localhost", proxyToPort.ToString(), requestTimeout); appBuilder.UseMiddleware <ConditionalProxyMiddleware>(publicPath, proxyOptions); }
public ConditionalProxyMiddleware( RequestDelegate next, string pathPrefix, ConditionalProxyMiddlewareOptions options) { if (!pathPrefix.StartsWith("/")) { pathPrefix = "/" + pathPrefix; } _next = next; _pathPrefix = pathPrefix; _pathPrefixIsRoot = string.Equals(_pathPrefix, "/", StringComparison.Ordinal); _options = options; _httpClient = new HttpClient(new HttpClientHandler()); _httpClient.Timeout = _options.RequestTimeout; }