【发布时间】:2019-07-20 12:11:15
【问题描述】:
我有一个 MEAN 应用。 角 CLI:7.1.4 节点:10.1.0 操作系统:win32 x64 角度:7.1.4
最近来自 HttpClientModule 的 http 请求一直被卡住并且没有发布到节点服务器: Img of chrome dev tools xhr pending request
nodejs 服务器(在本地和生产中运行(azure web 应用程序)并不表示它曾经收到过请求。这种情况不一致。有时它会完成,有时它只是挂起。 这是从 Angular 到服务器的 testConnection 调用的 sn-p:
角度服务
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { environment } from '../../environments/environment';
const Headers: any = { withCredentials: true, responseType: 'json', headers: { 'Content-Type': 'application/json' } };
@Injectable({
providedIn: 'root',
})
export class UserService {
constructor(private _http: HttpClient) {}
loginStatus() {
return this._http.get(`${environment.serverURL}/api/login-status`, Headers).pipe(catchError(this.handleError));
}}
角度组件:
ngOnInit() {
this._userSvc.loginStatus().subscribe(
(result:any)=>{console.log(result)},
(error:any)=>{console.log(error)})
}
节点/快递:
router.get('/login-status', (req, res, next) => {
if (req.isAuthenticated()) {
res.status(200).json(req.user);
} else {
res.status(403).json({
success: false,
error: 'User not Authenticated',
message: "Please return to the login in page and try again."
})
}
})
节点正在使用护照进行身份验证
不要纠结于护照问题,因为这条路线并不总是会失败。我有简单的路线,不进行验证,只返回一些失败的文本。
我尝试修改我的 CORS 选项,但我只能阻止自己。 有时重新启动服务器会允许请求完成,但并非总是如此。
【问题讨论】:
-
额外说明*使用邮递员,我每次都能成功调用服务器,我也可以使用浏览器访问不安全的获取路由。
-
显然您正在从任何组件订阅 loginStatus 对吗?
-
你能说明 ${environment.serverURL} 的解析吗?
-
这似乎是一个 RxJS 的东西,当你修改添加 .subscribe 的问题时可能会更清楚。当您在开发模式下为应用程序提供服务时会发生这种情况?:ng serve.... 或使用内置版本。无论如何,你能显示你正在运行的 ng serve/build 语句吗?
-
不,这里要记住的主要事情是它并不总是停止。偶尔而已。因此,不调用函数会使其每次都失败。此外,请求永远不会到达节点路由。