在 angualr 8 中处理长 Web 请求

问题描述

我有一个 angular 8 应用程序,当我发出长请求时,会抛出网关超时 504

 const sub = this.reportsService.getUsageReport(this.reportRequest)
       .subscribe((res: any) => {
       var blob = new Blob([(res)._body],{
         type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"
        });
         if(res.status == 204){
           alert(this.NoDataFoundMsg);
           this.loadingReport = false ;
         }
         else{
         FileSaver.saveAs(blob,"SE2_Usage_Report.xlsx");
         this.loadingReport = false ;
         }
      },error => {
        this.loadingReport = false ;
        this.handleError(sub);
      }
      )

getUsageReport() this service take a above 5 min because the returned data is huge so the time of request maybe is greater than 5 min,so When the time takes more than 5 minutes angular throws Gateway Timeout 504

解决方法

您可以在 Angular 应用程序的 proxy.conf.json 文件中添加超时配置。通过此链接 Proxying to a backend server

检查如何从 Angular 文档创建它

只需在 API 的 JSON 对象上添加一个密钥超时,配置秒数如下:

{
  "/api": {
      "target": "http://localhost:3000","secure": false,"timeout": 360000
  }
}

您也可以查看此答案How to increase HTTP request timeout more than 2 minutes in Angular 7?