【问题标题】:Handling long Web Requests In angualr 8在 Angular 8 中处理长 Web 请求
【发布时间】:2021-03-29 09:44:55
【问题描述】:

我有一个 Angular 8 应用程序,当我发出长请求时抛出 Gateway Timeout 504 有没有办法从 angular 端而不是服务器配置端避免这个异常?

 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

【问题讨论】:

  • 欢迎来到 StackOverflow,感谢您的发帖!您能否更新您的问题以提供触发超时的具体代码示例?这将帮助其他用户提供有用的答案。在这里查看更多帮助:stackoverflow.com/help/how-to-ask
  • 感谢您的评论,我已经修改了。
  • 最简单的答案是否定的,你不能。您只能使用某种队列,您可以在其中放置流程命令,生成报告,当报告准备好时,您应该通知您的客户端报告已准备好下载,您可以使用实时通信(signalR,socketIO, firestore 等...),或轮询,一切顺利
  • 504 是服务器端异常,表示服务器正在关闭连接;您无法从客户端更改它。

标签: angular http-status-code-504


【解决方案1】:

您可以在 Angular 应用程序的 proxy.conf.json 文件中添加超时配置。检查如何从这个链接Proxying to a backend server的角度文档创建它

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多