private static Dictionary <string, NgProxy> CreateProxiesAfterNgStarted(Dictionary <string, NgProxyOptions> pathPrefixToProxyOptionsMap)
        {
            // Do not assign an NgProxy until the Ng server is available.
            var pathPrefixToProxyMap = pathPrefixToProxyOptionsMap.Keys.ToDictionary(i => i, i => (NgProxy)null);

            // Create a proxy for each Ng app
            foreach (var item in pathPrefixToProxyOptionsMap)
            {
                var pathPrefix   = item.Key;
                var proxyOptions = item.Value;

                // Wait until the Ng server has started.
                Task.Factory.StartNew(async() =>
                {
                    do
                    {
                        await Task.Delay(TimeSpan.FromSeconds(2));
                    }while (IsPortAvailable(proxyOptions.Port));

                    var proxy = new NgProxy(proxyOptions);
                    // From now on we can proxy requests.
                    pathPrefixToProxyMap[pathPrefix] = proxy;
                }
                                      );
            }

            return(pathPrefixToProxyMap);
        }
        /// <summary>
        /// Ignore the request Path for Angular CLI versions prior to 1.4.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="proxy"></param>
        /// <returns></returns>
        private static async Task CallProxyToPathRoot(HttpContext context, NgProxy proxy)
        {
            var requestPath = context.Request.Path;

            try
            {
                if (requestPath.HasValue)
                {
                    var lastSeparatorIndex = requestPath.Value.LastIndexOf('/');
                    // If the slash is at the start, keep the path as is.
                    if (lastSeparatorIndex > 0)
                    {
                        var proxyPath = requestPath.Value.Substring(lastSeparatorIndex);
                        context.Request.Path = new PathString(proxyPath);
                    }
                }
                // Call the Ng server
                await proxy.HandleRequest(context);
            }
            finally
            {
                context.Request.Path = requestPath;
            }
        }