【发布时间】:2018-08-30 11:52:41
【问题描述】:
我正在尝试使用 axios 和 fs 下载图像
当我使用 node app.js 运行它时,它给了我一个错误,说管道
无法定义
“TypeError:无法读取未定义的属性‘管道’”。
const axios = require('axios');
const fs = require('fs');
const Path = require('path');
async function download()
{
const url ='https://www.google.co.in/imgres'
const path = Path.resolve(__dirname,'files','image1.jpg')
const response = axios
(
{
method : 'GET',
url:url,
responseType:'stream'
}
)
response.data.pipe(fs.createWriteStream(path))
return new Promise((resolve,reject)=>{
response.data.on('end',()=>{
resolve()
})
response.data.on('error',err=>{
reject(err);
})
}).catch();
}
download().then(()=>{
console.log('download finished');
})
【问题讨论】: