Socket
Socket
Sign inDemoInstall

follow-redirects

Package Overview
Dependencies
Maintainers
2
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

follow-redirects

HTTP and HTTPS modules that follow redirects.


Version published
Weekly downloads
45M
increased by9.23%
Maintainers
2
Weekly downloads
 
Created

What is follow-redirects?

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.

What are follow-redirects's main functionalities?

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);
});

Other packages similar to follow-redirects

Keywords

FAQs

Package last updated on 03 Sep 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc