![](/img/trans.png)
[英]why is axios get + post request timing out after 5 button requests?
[英]Axios get request working locally but timing out on serverless
我正在嘗試抓取一些網站,但由於某種原因,它可以在本地(本地主機)使用 express 工作,但在我將其部署到 lambda 時則不行。 嘗試使用 ff serverless-http 和 aws-serverless-express 和 serverless-express 插件。 還嘗試在 axios 和 superagent 之間切換。
路線工作正常,經過數小時的調查,我已將問題縮小到 fetch/axios 位。 當我不向 axios/superagent/etc 添加超時時,應用程序只會繼續運行並在 15/30 秒時超時,以設置為准並收到錯誤 50*。
service: scrape
provider:
name: aws
runtime: nodejs10.x
stage: dev
region: us-east-2
memorySize: 128
timeout: 15
plugins:
- serverless-plugin-typescript
- serverless-express
functions:
app:
handler: src/server.handler
events:
- http:
path: /
method: ANY
cors: true
- http:
path: /{proxy+}
method: ANY
cors: true
protected async fetchHtml(uri: string): Promise<CheerioStatic | null> {
const htmlElement = await Axios.get(uri, { timeout: 5000 });
if(htmlElement.status === 200) {
const $ = Cheerio.load(htmlElement && htmlElement.data || '');
$('script').remove();
return $;
}
return null;
}
據我所知,axios 的默認超時時間是不確定的。 請記住,API 網關有 29 秒超時的硬限制。
我最近遇到了同樣的問題,有時超時是由於冷啟動。 所以我基本上不得不在我的前端反應應用程序中為 api 調用添加一個重試邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.