以角度 8 处理长 Web 请求
Handling long Web Requests In angualr 8
我有一个 angular 8 应用程序,当我发出长请求时抛出网关超时 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
您可以在 Angular 应用程序的 proxy.conf.json
文件中添加超时配置。从 link Proxying to a backend server
的 angular 文档中检查如何创建它
只需在 API 的 JSON 对象上添加一个键超时,配置的秒数如下:
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"timeout": 360000
}
}
您也可以查看此答案 如何在 Angular 7 中将 HTTP 请求超时增加超过 2 分钟?
我有一个 angular 8 应用程序,当我发出长请求时抛出网关超时 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
您可以在 Angular 应用程序的 proxy.conf.json
文件中添加超时配置。从 link Proxying to a backend server
只需在 API 的 JSON 对象上添加一个键超时,配置的秒数如下:
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"timeout": 360000
}
}
您也可以查看此答案 如何在 Angular 7 中将 HTTP 请求超时增加超过 2 分钟?