node-wget-fetch
Ultra simple async retrieval of resources or remote files over http or https, an cli tool, and convenience wrapper of node-fetch.
Install
npm install node-wget-fetch
Basic API
fetching(url
, action = destination
| response_body_type
| options
[, options
])
-
url
A string representing an absolute url
-
action
Save to destination or body action on response type or use for options
response type can be:
- '
header
' for all response headers - raw() - '
object
' for the response object - no post/pre processing - '
array
' for arrayBuffer() - '
buffer
' for buffer() - '
blob
' for blob() - '
json
' for json() - '
text
' for text() - '
converted
' for textConverted() - '
stream
' for NodeJs.readableStream()
default is 'download
'
-
options
Standard Request/Fetch Options for the HTTP(S) request
-
Returns: Promise of response body
of above type, only if status text is OK
-
The response type will set Fetch/Request header 'Content-Type'
as:
- '
json
' = 'application/json; charset=utf-8' - '
text
' = 'application/x-www-form-urlencoded' - '
blob
' = 'application/octet' - '
buffer
' = 'application/octet' - '
header
' = 'text/plain' - '
object
' = 'application/json; charset=utf-8' - '
stream
' = 'application/octet' - '
array
' = 'application/octet' - '
converted
' = 'application/x-www-form-urlencoded'
Convenience Request Methods
fetching.get(url
, action = response_body_type
| options
[, options
])
fetching.head(url
, action = response_body_type
| options
[, options
])
fetching.options(url
, action = response_body_type
| options
[, options
])
For simply submitting body
data
fetching.post(url
, body
, action = response_body_type
| options
[, options
])
fetching.put(url
, body
, action = response_body_type
| options
[, options
])
fetching.patch(url
, body
, action = response_body_type
| options
[, options
])
fetching.delete(url
, body
, action = response_body_type
| options
[, options
])
Bring in or access node-fetch directly
fetching.fetch(url
[, options
])
Usage
const fetching = require('node-wget-fetch');
fetching.wget(url)
.then((info) => {});
.catch((error) => {});
fetching.wget(url, { headers: { Accept: '*/*' } })
.then((info) => {});
.catch((error) => {});
fetching.wget(url, destination_folder_or_filename, { timeout: 2000 } )
.then((info) => {});
.catch((error) => {});
fetching(url, responseType,
{ headers: {Accept: '*/*' } })
)
.then((processedResponse) => {
});
.catch((error) => {});
Examples
const wget = require('node-wget-fetch');
wget('https://raw.github.com/techno-express/node-wget-fetch/master/angleman.png');
wget('https://raw.github.com/techno-express/node-wget-fetch/master/package.json',
'/tmp/',
{ timeout: 2000 }
)
.then((info) => {
console.log('--- headers:');
console.log(info.headers);
console.log('--- file path:');
console.log(info.filepath);
console.log('--- file size retrieved:');
console.log(info.fileSize);
console.log('--- Do file retrieved match "Content-Length"?:');
console.log(info.fileSizeMatch);
})
.catch((error) => {
console.log('--- error:');
console.log(error);
});
);
CLI
Install:
npm install -g node-wget-fetch
Use:
Usage: wget [options] <url>
Or
Usage: fetch [options] <url>
Ultra simple async retrieval of remote files over http or https
Options:
-h, --help output usage information
-v, --version output version number
-d, --destination <folder> specify download destination
Usage:
# Download file
$ wget https://github.com/NodeOS/NodeOS/archive/master.zip
$ fetch https://github.com/NodeOS/NodeOS/archive/master.zip
# Download file to location
$ wget https://github.com/NodeOS/NodeOS/archive/master.zip -d path/to/here/
$ fetch https://github.com/NodeOS/NodeOS/archive/master.zip -d path/to/here/
License: MIT