NodeJS Request: How do I get `response.statusCode` when piping?
You need to catch the response
event from request
. It won't be in the finished
event from the pipe. You should also consider catching errors before the pipe. This is where you'll catch network errors. After the pipe you'll catch file errors.
request(url).on('response', function(response) { console.log(response.statusCode) // <--- Here 200}).on("error", function(err){ console.log("Problem reaching URL: ", err); }).pipe(destination).on("finish", function(response) { console.log("done");}).on("error", function(err){ console.log("Problem writing file: ", err);});