简体   繁体   English

Axios 请求在本地工作但在无服务器上超时

[英]Axios get request working locally but timing out on serverless

I'm trying to scrape some websites, but for some reason it works locally (localhost) with express, but not when I've deployed it to lambda.我正在尝试抓取一些网站,但由于某种原因,它可以在本地(本地主机)使用 express 工作,但在我将其部署到 lambda 时则不行。 Tried w/ the ff serverless-http and aws-serverless-express and serverless-express plugin.尝试使用 ff serverless-http 和 aws-serverless-express 和 serverless-express 插件。 Also tried switching between axios and superagent.还尝试在 axios 和 superagent 之间切换。

Routes work fine, and after hrs of investigating, I've narrowed the problem down to the fetch/axios bit.路线工作正常,经过数小时的调查,我已将问题缩小到 fetch/axios 位。 When i don't add a timeout to axios/superagent/etc, the app just keeps running and timing out at 15/30 sec, whichever is set and get an error 50*.当我不向 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;
    }

As far as i know, the default timeout of axios is indefinite.据我所知,axios 的默认超时时间是不确定的。 Remember, API gateway has hard limit of 29 sec timeout.请记住,API 网关有 29 秒超时的硬限制。

I had the same issue recently, sometimes the timeouts are due to cold starts.我最近遇到了同样的问题,有时超时是由于冷启动。 So I basically had to add a retry logic for the api call in my frontend react application.所以我基本上不得不在我的前端反应应用程序中为 api 调用添加一个重试逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM