【问题标题】:Pipe image using node-request but abort on non-200 http status code使用节点请求的管道图像,但在非 200 http 状态码上中止
【发布时间】:2018-03-12 09:21:12
【问题描述】:

我正在尝试使用 node-request 对存储在 Amazon S3 上的图像进行管道传输。但是,有时图像不存在,S3 将其公开为状态码 403。我正在努力了解如何在成功的情况下进行管道传输(200),但在非 200 的情况下采取替代操作.

使用 abort() 方法似乎是可行的方法,但会得到一个 r.abort is not a function,即使它应该在请求中可用。

// context: inside a request handler, where `res` is the response to write to
const r = request(url)
  .on('response', function (response) {
    if (response.statusCode !== 200) {
      r.abort(); //failing
      // I want to stop the piping here and do whatever instead.
    }
  })
  .pipe(res);

想法?

【问题讨论】:

    标签: node.js node-request


    【解决方案1】:

    回答我自己的问题:在确定正确之前不要开始管道:

     request(url)
        .on('response', function (r) {
          if (r.statusCode !== 200) {
            //take other action
          }
          r.pipe(res);
        })
    

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2016-01-11
      • 2013-02-18
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2023-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多