Node-request - How to determine if an error occurred during the request?

前端 未结 4 1877
栀梦
栀梦 2020-12-24 12:20

I\'m trying to leverage the node-request module, but the documentation isn\'t that great. If I make a request to a valid resource and pipe it to a Writable Stream, everythin

4条回答
  •  [愿得一人]
    2020-12-24 12:35

    @Mikeal solution looks great, but may have some problem with the piping (first few bytes can be missed). Hee is an updated code:

    var r = request(url)
    r.pipe(new WritableStream());
    r.on('response', function (resp) {
       resp.headers 
       resp.statusCode
       // Handle error case and remove your writablestream if need be.
    })
    

提交回复
热议问题