nodejs用superangent下载图片,检查文件大小

nodejs download image with superangent ,check the file size

...
superagent
            .get(req.body.img)
            .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36')
            .pipe(fs.createWriteStream(filePath));
console.log(fs.statSync(filePath));
...

输出是 {"dev":2049,"mode":33204,"nlink":1,"uid":1000,"gid":1000,"rdev":0,"blksize":4096,"ino":6578368,"size":0,"blocks":0,"atime":"2016-07-18T02:46:12.845Z", "mtime":"2016-07-18T02:46:12.845Z","ctime":"2016-07-18T02:46:12.845Z","birthtime":"2016-07-18T02 :46:12.845Z"}

为什么文件大小为 0,我已经检查了文件大小:

console.log(fs.statSync(filePath));

{ 开发: 2049, 模式:33204, 链接:1, 用户标识符:1000, gid: 1000, 开发:0, 块大小:4096, 编号:1975657, 尺寸:1964074, 块:3840, 时间:2016-07-18T02:29:16.977Z, 时间:2016-07-18T02:28:30.037Z, ctime:2016-07-18T02:28:30.037Z, 出生时间:2016-07-18T02:28:30.037Z }

也许,您在流式写入文件时调用了 console.log。尝试在 finish.

上添加侦听器
var stream = fs.createWriteStream(filePath);
stream.on('finish', function () {
    console.log(fs.statSync(filePath)); 
});

superagent 
    ...
    .pipe(stream);