nodejs request module check for response status before pipe into file

I am currently using "request" module to download some files.

The code looks like

function download(url, fileName, path) {     return new Promise(function(resolve, reject) {         request({url: url})         .pipe(fs.createWriteStream(path))         .on("close", function() {             log.d("File %s downloaded to path %s", fileName, path);             resolve(info);         })         .on("error", reject);     }); } 

Now, I notice that even when the URL returns a 404 NOT FOUND. Somehow the request module still manages to pipe out something and a file with 1 kb is stored.

What can I do to intercept the status code, maybe check that it is less than 400. Before piping it to file?

I look up their github, couldn't find an example.

Thanks a lot.


Category: node.js Time: 2016-07-29 Views: 0
Tags: node.js

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.133 (s). 12 q(s)