我对Angular 6 Universal App有问题。乍一看,一切看起来都不错,但是当我查看服务器端的日志时,会看到很多:
ERROR [Error]
ERROR [Error]
ERROR [Error]
ERROR [Error]
ERROR [Error]
ERROR [Error]
ERROR [Error]
ERROR [Error]
ERROR [Error]
有什么方法可以在显示错误时启动具有更多详细信息的应用程序?
我从"dev"
中的脚本package.json
启动应用程序:
"scripts": {
"ng": "ng",
"prestart": "npm run build:prod",
"start": "node $npm_package_main",
"build": "ng run ng-universal:udk",
"build:prod": "ng run ng-universal:udk:production",
"dev": "ng-udkc",
"dev:spa": "ng serve --hmr",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"now-build": "ng run ng-universal:udk:production && gulp build-zip",
"gulp": "gulp build-zip",
"now-start": "node $npm_package_main"
},
答案 0 :(得分:1)
几天前,我遇到了同样的问题。 这些错误是返回非200状态代码的HTTP请求。
有什么方法可以在显示错误时启动具有更多详细信息的应用程序?
从NodeJs的角度来看,我认为没有。 Altough Universal尝试运行有角度的应用程序,就像在浏览器中执行一样。 因此,您可以添加一个HTTP拦截器,并在拦截器中添加一些控制台日志,以找出哪个URL没有响应。
这里有个例子:
export class HeadersInterceptor implements HttpInterceptor {
//private translateService: TranslateService;
constructor(@Inject(WINDOW) private window: Window, @Inject(LOCAL_STORAGE) private localStorage: any, private translateService: TranslateService, @Inject(PLATFORM_ID) private platform) {
/*this.translateService = this.injector.get(TranslateService);*/
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(req);
console.log(req.url);
return next.handle(req);
}
不要忘记将其添加到模块中。
core.module.ts中的示例:
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: HeadersInterceptor,
multi: true,
},
]
我希望这会有所帮助。
编辑:
“我的解决方案”将不会向您提供错误原因的详细信息。我考虑了一下,您当然可以做类似的事情:
RxJS <6:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do((event: HttpEvent<any>) => {}, (err: any) => {
if (err instanceof HttpErrorResponse) {
// do error handling here
}
});
}
RxJS> = 6
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(tap((success) => {
}, (err) => {
if (err instanceof HttpErrorResponse) {
console.log(err.status);
}
}
));
}
然后,您应该可以访问该错误。 此外,此代码是从hackernoon复制的:https://hackernoon.com/global-http-error-catching-in-angular-4-3-9e15cc1e0a6b