Skip to content

Instantly share code, notes, and snippets.

@viniciusgati
Created December 6, 2019 16:52
Show Gist options
  • Select an option

  • Save viniciusgati/73f80de6f6c77fd457e278d1e30db6aa to your computer and use it in GitHub Desktop.

Select an option

Save viniciusgati/73f80de6f6c77fd457e278d1e30db6aa to your computer and use it in GitHub Desktop.
'use strict'
const Fs = require('fs')
const Path = require('path')
const Axios = require('axios')
async function downloadImage () {
const url = 'https://unsplash.com/photos/AaEQmoufHLk/download?force=true'
const path = Path.resolve(__dirname, 'images', 'code.jpg')
const writer = Fs.createWriteStream(path)
const response = await Axios({
url,
method: 'GET',
responseType: 'stream'
})
response.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
}
downloadImage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment