Skip to content

ctx.curl 请求图片遇到的一些问题 #1341

@jinyang1994

Description

@jinyang1994

需求是这样的:

在egg中请求一张图片的buffer。

代码中是这样写的:

module.exports = app => {
  class Test extends app.Service {
    * getImageBuffer(url) {
      const { ctx } = this;
      const result = yield ctx.curl(url, {
        timeout: 20000,
      });
      
      return result.data;
    }
  }

  return Test;
}

实际遇到的问题:

在本地开发的时候,可以获取到图片的buffer,
但是在线上环境。请求就会报出以下错误。

2017-08-22 13:10:08,747 ERROR 16631 [-/182.201.174.232/-/72184ms POST /api/v1/event/image?id=22] nodejs.RequestError: socket hang up (req "error"), GET http://24liveblog.tradingfront.cn/watermark/e4da3b7fbbce2345d7772b0674a318d5.png -1 (connected: false, keepalive socket: false, agent status: {"createSocketCount":9,"createSocketErrorCount":0,"closeSocketCount":9,"errorSocketCount":0,"timeoutSocketCount":9,"requestCount":8,"freeSockets":{},"sockets":{},"requests":{}})
headers: {}
headers: {}
    at createHangUpError (_http_client.js:345:15)
    at Socket.socketCloseListener (_http_client.js:377:23)
    at emitOne (events.js:120:20)
    at Socket.emit (events.js:210:7)
    at TCP._handle.close [as _onclose] (net.js:549:12)
code: 'ECONNRESET'
name: 'RequestError'
data: undefined
path: '/watermark/e4da3b7fbbce2345d7772b0674a318d5.png'
status: -1
headers: {}
res: { status: -1, statusCode: -1, headers: {}, size: 0, aborted: false, rt: 30004, keepAliveSocket: false, data: undefined, requestUrls: [ 'http://24liveblog.tradingfront.cn/watermark/e4da3b7fbbce2345d7772b0674a318d5.png' ], timing: null, remoteAddress: '', remotePort: '' }
pid: 16631
hostname: li1242-25.members.linode.com

另外补充:

图片的路径可以尝试使用: https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503414433116&di=75e06c91392d4b185a33dc9785a02587&imgtype=0&src=http%3A%2F%2Fimage.tupian114.com%2F20140718%2F11020173.jpg.thumb.jpg
对于httpClient的相关配置都是默认的。

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions