I have an API that I'm running locally on my machine. I'm trying to access this data in a development environment and running into issues.
When I have my React Native app running through expo, localhost is referring to the devices localhost and not the one running on my desktop. I have researched this quite a bit and the top suggestion is to query your internal IP address instead of localhost.
When I query localhost I get an exception thrown with no details:
Network request failed
When I query my internal IP, the request hangs and nothing happens.
I'm starting expo through this command: expo start --tunnel
Here is the code making the request:
fetch("http://localhost:5001/api/standings")
.then((res) => console.log(res))
.catch((err) => console.log(err))
The .NET API works through chrome. On both localhost and my internal IP (with a warning about an unsecure connection).
I there any additional configuration I'm missing?
EDIT:
Here is the Postman response:
Here is my Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddHttpClient();
services.AddCors();
services.AddEnv((builder) =>
{
builder
.AddEnvFile(".env")
.AddThrowOnError(true)
.AddEncoding(Encoding.ASCII);
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
app.UseHttpsRedirection();
app.UseMvc();
}
}
