What is miniget?
miniget is a lightweight HTTP request library for Node.js, primarily used for making simple GET requests. It is designed to be minimalistic and easy to use, focusing on simplicity and performance.
What are miniget's main functionalities?
Basic GET Request
This feature allows you to make a basic GET request to a specified URL and handle the response as text. The code sample demonstrates how to fetch a JSON object from a placeholder API and log it to the console.
const miniget = require('miniget');
miniget('https://jsonplaceholder.typicode.com/todos/1').text().then(console.log).catch(console.error);
Stream Response
This feature allows you to stream the response directly to a writable stream, such as a file. The code sample demonstrates how to fetch a JSON object from a placeholder API and save it to a file named 'response.json'.
const miniget = require('miniget');
const fs = require('fs');
miniget('https://jsonplaceholder.typicode.com/todos/1').pipe(fs.createWriteStream('response.json'));
Custom Headers
This feature allows you to specify custom headers for your GET request. The code sample demonstrates how to set a custom 'User-Agent' header when making a request to a placeholder API.
const miniget = require('miniget');
const options = {
headers: {
'User-Agent': 'my-custom-agent'
}
};
miniget('https://jsonplaceholder.typicode.com/todos/1', options).text().then(console.log).catch(console.error);
Other packages similar to miniget
axios
Axios is a promise-based HTTP client for the browser and Node.js. It provides a more comprehensive set of features compared to miniget, including support for POST requests, request and response interceptors, and automatic transformation of JSON data. It is more feature-rich but also larger in size.
node-fetch
node-fetch is a lightweight module that brings `window.fetch` to Node.js. It is similar to miniget in terms of simplicity and ease of use but offers a more familiar API for those who have used the Fetch API in the browser. It supports a wider range of HTTP methods and is more versatile.
got
Got is a human-friendly and powerful HTTP request library for Node.js. It offers a rich set of features, including retries, streams, and advanced configuration options. Compared to miniget, Got is more robust and suitable for more complex use cases, but it is also larger and more complex.
node-miniget
A small http(s) GET library with redirects, retries, reconnects, concatenating or streaming, and no dependencies. This keeps filesize small for potential browser use.
Usage
Concatenates a response
const miniget = require('miniget');
miniget('http://mywebsite.com', (err, res, body) => {
console.log('webpage contents: ', body);
});
let body = await miniget('http://yourwebsite.com').text();
Request can be streamed right away
miniget('http://api.mywebsite.com/v1/messages.json')
.pipe(someWritableStream());
API
miniget(url, [options])
Makes a GET request. options
can have any properties from the http.request()
function, in addition to
If you'd like a concatenated response, use miniget(url).text()
.
let body = await miniget('http://yourwebsite.com').text();
Miniget returns a readable stream, errors will then be emitted on the stream. Returned stream has additional methods added, and can emit the following events.
Stream#abort()
Aborts the request.
Stream#text()
Returns a promise that resolves to the concatenated contents of the response.
Event: redirect
string
- URL redirected to.
Emitted when the request was redirected with a redirection status code.
Event: retry
number
- Number of retry.Error
- Request or status code error.
Emitted when the request fails, or the response has a status code >= 500.
Event: reconnect
number
- Number of reconnect.Error
- Request or response error.
Emitted when the request or response fails after download has started.
Event: request
Emitted when a video request is made, including after any redirects, retries, and reconnects.
Event: response
Emitted when a video response has been found and has started downloading, including after any successful reconnects.
Forwarded events
Any events emitted from the request or response objects will be forwarded to the miniget stream.
Install
npm install miniget
Tests
Tests are written with mocha
npm test