我创建了一个Angular App,在我的开发机器上运行得很好。 现在,我正在尝试将其安装在Windows生产服务器上。 为此,我安装了apache以便为该应用程序以及随附的Rest Service API提供服务。 如果我打开网络浏览器和服务器,并通过公共IP(https)使用Angular应用程序,则它运行良好。 如果我从另一台计算机上进行同样的操作,则可以进入第一个登录页面,但是当我要登录时,会收到以下错误消息: ->选项http://localhost:5001/users/authenticate净:: ERR_EMPTY_RESPONSE
我尝试配置防火墙,但这无济于事,我读了很多主题,但找不到任何答案。
其余API用C#完成,启动Configure函数如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
Console.WriteLine("Hello Welcome to WebAPI service");
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
//.WithOrigins( "http://localhost:80" )
//.SetIsOriginAllowed( isOriginAllowed: _ => true )
.AllowCredentials());
app.UseAuthentication();
app.UseMvc();
}
从Angular App对API的调用如下:
return this.http.post<any>(`${this.env.apiUrl}/users/authenticate`, { username, password })
.pipe(map(user => {
// login successful if there's a jwt token in the response
if (user && user.token) {
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(user));
this.currentUserSubject.next(user);
}
return user;
}));
env.js文件:
(function (window) {
window.__env = window.__env || {};
// API url
window.__env.apiUrl = 'http://localhost:5001';
// Whether or not to enable debug mode
// Setting this to false will disable console output
//window.__env.enableDebug = true;
}(this));
答案 0 :(得分:0)
我们可以看到您的环境文件吗?
我的猜测是,您需要将env.apiUrl
变量设置为公用域名或IP,而不是本地主机。
API请求是从客户端而不是服务器调用的。因此,当用户访问您的网站时,他将从他的计算机中调用位于您服务器上的API。因此,您需要将env.apiUrl
设置为具有正确端口的服务器的域或IP地址。
答案 1 :(得分:0)
您的Angular应用正在运行客户端。因此,由于客户端不会在本地运行后端,因此无法通过localhost
调用API。
由于您的后端正在计算机上运行,因此在开发应用程序时它一直在工作。它也可以在您的服务器上运行,因为据我了解,您的后端和Angluar App位于同一服务器上。
首先想做的是测试您的服务器上是否正确配置了API,以便从本地计算机上用postman运行它以检查它是否可以访问。
如果正常运行,则必须将env.apiUrl
设置为服务器的公共IP地址,并使用正确的端口号。