Node Image Downloader
A Node module for downloading image to disk from a given URL
Install
npm install --save image-downloader
Options
- url (required) - the image URL to download
- dest (required) - the image destination. Can be a directory or a
filename. If a directory is given, ID will automatically extract the image
filename from
options.url
(see usage bellow) - extractFilename - boolean indicating whether the image filename will be
automatically extracted from
options.url
or not. Set to false
to have
options.dest
without a file extension for example. (default: true
) - headers - HTTP headers (default:
{}
) - timeout - milliseconds before a request times out
- maxRedirects - the maximum number of allowed redirects; if exceeded, an
error will be emitted. (default:
21
)
For advanced options, see Node.js http.request()
's options documentation
Usage
Download to a directory and save with the original filename
const download = require('image-downloader');
const options = {
url: 'http://someurl.com/image.jpg',
dest: '/path/to/dest',
};
download.image(options)
.then(({ filename }) => {
console.log('Saved to', filename);
})
.catch((err) => console.error(err));
Download to a directory and save with an another filename
const download = require('image-downloader');
options = {
url: 'http://someurl.com/image2.jpg',
dest: '/path/to/dest/photo.jpg',
};
download.image(options)
.then(({ filename }) => {
console.log('Saved to', filename);
})
.catch((err) => console.error(err));
Download with another filename without extension
const download = require('image-downloader');
options = {
url: 'http://someurl.com/image3.jpg',
dest: '/path/to/dest/photo',
extractFilename: false,
};
download.image(options)
.then(({ filename }) => {
console.log('Saved to', filename),
})
.catch((err) => console.error(err));
Development Setup
-
Install Nix Package Manager
-
Install direnv
with your OS package manager
-
Hook it direnv
into your shell
-
At the top-level of your project run:
direnv allow
The next time your launch your terminal and enter the top-level of your
project, direnv
will check for changes.
Contribute
Please follow CONTRIBUTING.md.
License
Under the MIT license. See LICENSE file for more details.