Skip to content

Instantly share code, notes, and snippets.

@hungdev
Last active April 5, 2019 13:27
Show Gist options
  • Save hungdev/ea8f963dd1818a30164052c72e5016c0 to your computer and use it in GitHub Desktop.
Save hungdev/ea8f963dd1818a30164052c72e5016c0 to your computer and use it in GitHub Desktop.
node download
'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