March 28, 2021
node-fetch
is a great package that allows you to use the wonderful fetch
API in server-side Node.js. It can also be used to request and download images from a URL to the local filesystem!
Here's how you do it, assuming that you have node-fetch
already installed ( by running npm install node-fetch
).
const fs = require('fs') // Built-in filesystem package for Node.js
const fetch = require('node-fetch')
const imageUrl = 'https://via.placeholder.com/350x150'
fetch(imageUrl)
.then(res =>
res.body.pipe(fs.createWriteStream('./path/to/image.png'))
)
This downloads the image at imageUrl
and "pipes" (downloads) it to the given path.
Note: Make sure that the folder you're downloading into exists already. If it doesn't, you'll want to create the folders required by doing something like:
const fs = require('fs')
fs.mkdirSync('./path/to', { recursive: true })
Tufts Meal Plan Wrapped
Mar 2, 2024
Building an e-ink picture frame that displays an iCloud photo album
Jan 9, 2024
2023 in review
Jan 5, 2024
Subscribe to my newsletter for a monthly round-up of new blog posts and projects Iām working on!