NPM follow-redirects Package
Follow-Redirects NPM Package
Overview
The follow-redirects
NPM package allows you to easily follow HTTP 3xx redirects in Node.js. It seamlessly handles multiple redirects and provides a consistent response object, regardless of the number of redirects.
Installation
To install the package, run the following command in your terminal:
npm install follow-redirects
Usage
Basic Usage
The package exports a single function followRedirects
that takes a request object as its first argument and returns a promise that resolves to a response object. You can use this function to follow HTTP 3xx redirects in your application.
const request = require('request');
const {followRedirects} = require('follow-redirects');
request('https://google.com', (err, res, body) => {
followRedirects(request, res).then((response) => {
console.log(response.body);
});
});
Custom Configuration
You can customize the behavior of the followRedirects
function by passing an options object as its second argument. The following options are available:
maxRedirects
: The maximum number of redirects to follow. The default is10
.timeout
: The amount of time to wait for a response before timing out. The default is10000
milliseconds.strictSSL
: Iftrue
, the request will fail if the destination server does not provide a valid SSL certificate. The default isfalse
.
const request = require('request');
const {followRedirects} = require('follow-redirects');
request('https://example.com', (err, res, body) => {
followRedirects(request, res, {
maxRedirects: 5,
timeout: 5000,
strictSSL: true,
}).then((response) => {
console.log(response.body);
});
});
Best Practices
- Use the
maxRedirects
option to limit the number of redirects followed to prevent infinite loops. - Set a reasonable
timeout
value to avoid waiting indefinitely for a response. - When following redirects to a different domain, verify the SSL certificate to ensure the connection is secure.
Contributing
Contributions are welcome! Please read the contributing guidelines before submitting a pull request.