Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
follow-redirects
Advanced tools
The follow-redirects npm package is a drop-in replacement for Node.js' native http and https modules that automatically follows HTTP(S) redirects. It provides an easy way to make HTTP(S) requests without having to manually handle redirection logic.
HTTP/HTTPS request with automatic redirection
This code demonstrates how to make a simple HTTP GET request that automatically follows redirects using the follow-redirects package.
const http = require('follow-redirects').http;
http.get('http://example.com', (response) => {
response.on('data', (chunk) => {
console.log(chunk.toString());
});
}).on('error', (err) => {
console.error(err);
});
Customizing redirect options
This code snippet shows how to customize the behavior of follow-redirects by setting the maximum number of redirects to follow and adding a hook to log the URL before redirecting.
const https = require('follow-redirects').https;
const options = {
maxRedirects: 10,
beforeRedirect: (options, { headers }) => {
console.log(`Redirecting to: ${options.hostname}${options.path}`);
}
};
https.get('https://example.com', options, (response) => {
// Handle response
}).on('error', (err) => {
console.error(err);
});
Streaming response data
This example demonstrates how to stream data from an HTTP GET request to a file, which is useful for downloading files while following redirects.
const http = require('follow-redirects').http;
const fs = require('fs');
const file = fs.createWriteStream('downloaded_file.txt');
http.get('http://example.com/file', (response) => {
response.pipe(file);
}).on('error', (err) => {
console.error(err);
});
Axios is a promise-based HTTP client for the browser and Node.js that supports automatic redirection. It provides a more feature-rich API compared to follow-redirects, including interceptors, request cancellation, and protection against XSRF.
Request is a simplified HTTP request client that supports redirection by default. It is no longer maintained, but it was once a popular choice for making HTTP requests in Node.js. It offered a higher-level API with convenience methods and support for forms and multipart file uploads.
Got is a human-friendly and powerful HTTP request library for Node.js. It handles redirections by default and provides a wide range of options for customization, retries, streams, and more. It is designed to be a more modern and feature-rich alternative to other HTTP request libraries.
Node-fetch is a light-weight module that brings the Fetch API to Node.js. It follows redirects by default and aims to provide a consistent API with the browser's fetch function. It is a good choice for those who prefer the Fetch API's promise-based syntax.
follow-redirects
extends http and https with the ability to follow
HTTP redirects painlessly. It does not modify the native modules but
instead offers its own http/https modules which inherit from the native
modules. If you want to automatically follow redirects, all you need to
do is replace:
var http = require('http');
by
var http = require('follow-redirects').http;
npm install follow-redirects
var http = require('follow-redirects').http;
var https = require('follow-redirects').https;
/*
* http and https are just like Node.js' http and https modules except
* that they follow redirects seamlessly.
*/
http.get('http://bit.ly/900913', function (res) {
res.on('data', function (chunk) {
console.log(chunk);
});
}).on('error', function (err) {
console.error(err);
});
/*
* You can optionnally pass the maxRedirect option which defaults to 5
*/
https.request({
host: 'bitly.com',
path: '/UHfDGO',
maxRedirects: 3
}, function (res) {
res.on('data', function (chunk) {
console.log(chunk);
});
}).on('error', function (err) {
console.error(err);
});
FAQs
HTTP and HTTPS modules that follow redirects.
The npm package follow-redirects receives a total of 42,616,422 weekly downloads. As such, follow-redirects popularity was classified as popular.
We found that follow-redirects demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.