59e31a43d1f19271772bbe21014c277a078ba3732004b2ca68693e4f9e60663def41b4c5ac8e36056c76587d391ddf3a22be2e29053143e36b150464d25c5f 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ## Follow Redirects
  2. Drop-in replacement for Node's `http` and `https` modules that automatically follows redirects.
  3. [![npm version](https://img.shields.io/npm/v/follow-redirects.svg)](https://www.npmjs.com/package/follow-redirects)
  4. [![Build Status](https://github.com/follow-redirects/follow-redirects/workflows/CI/badge.svg)](https://github.com/follow-redirects/follow-redirects/actions)
  5. [![Coverage Status](https://coveralls.io/repos/follow-redirects/follow-redirects/badge.svg?branch=master)](https://coveralls.io/r/follow-redirects/follow-redirects?branch=master)
  6. [![npm downloads](https://img.shields.io/npm/dm/follow-redirects.svg)](https://www.npmjs.com/package/follow-redirects)
  7. [![Sponsor on GitHub](https://img.shields.io/static/v1?label=Sponsor&message=%F0%9F%92%96&logo=GitHub)](https://github.com/sponsors/RubenVerborgh)
  8. `follow-redirects` provides [request](https://nodejs.org/api/http.html#http_http_request_options_callback) and [get](https://nodejs.org/api/http.html#http_http_get_options_callback)
  9. methods that behave identically to those found on the native [http](https://nodejs.org/api/http.html#http_http_request_options_callback) and [https](https://nodejs.org/api/https.html#https_https_request_options_callback)
  10. modules, with the exception that they will seamlessly follow redirects.
  11. ```javascript
  12. const { http, https } = require('follow-redirects');
  13. http.get('http://bit.ly/900913', response => {
  14. response.on('data', chunk => {
  15. console.log(chunk);
  16. });
  17. }).on('error', err => {
  18. console.error(err);
  19. });
  20. ```
  21. You can inspect the final redirected URL through the `responseUrl` property on the `response`.
  22. If no redirection happened, `responseUrl` is the original request URL.
  23. ```javascript
  24. const request = https.request({
  25. host: 'bitly.com',
  26. path: '/UHfDGO',
  27. }, response => {
  28. console.log(response.responseUrl);
  29. // 'http://duckduckgo.com/robots.txt'
  30. });
  31. request.end();
  32. ```
  33. ## Options
  34. ### Global options
  35. Global options are set directly on the `follow-redirects` module:
  36. ```javascript
  37. const followRedirects = require('follow-redirects');
  38. followRedirects.maxRedirects = 10;
  39. followRedirects.maxBodyLength = 20 * 1024 * 1024; // 20 MB
  40. ```
  41. The following global options are supported:
  42. - `maxRedirects` (default: `21`) – sets the maximum number of allowed redirects; if exceeded, an error will be emitted.
  43. - `maxBodyLength` (default: 10MB) – sets the maximum size of the request body; if exceeded, an error will be emitted.
  44. ### Per-request options
  45. Per-request options are set by passing an `options` object:
  46. ```javascript
  47. const url = require('url');
  48. const { http, https } = require('follow-redirects');
  49. const options = url.parse('http://bit.ly/900913');
  50. options.maxRedirects = 10;
  51. options.beforeRedirect = (options, response, request) => {
  52. // Use this to adjust the request options upon redirecting,
  53. // to inspect the latest response headers,
  54. // or to cancel the request by throwing an error
  55. // response.headers = the redirect response headers
  56. // response.statusCode = the redirect response code (eg. 301, 307, etc.)
  57. // request.url = the requested URL that resulted in a redirect
  58. // request.headers = the headers in the request that resulted in a redirect
  59. // request.method = the method of the request that resulted in a redirect
  60. if (options.hostname === "example.com") {
  61. options.auth = "user:password";
  62. }
  63. };
  64. http.request(options);
  65. ```
  66. In addition to the [standard HTTP](https://nodejs.org/api/http.html#http_http_request_options_callback) and [HTTPS options](https://nodejs.org/api/https.html#https_https_request_options_callback),
  67. the following per-request options are supported:
  68. - `followRedirects` (default: `true`) – whether redirects should be followed.
  69. - `maxRedirects` (default: `21`) – sets the maximum number of allowed redirects; if exceeded, an error will be emitted.
  70. - `maxBodyLength` (default: 10MB) – sets the maximum size of the request body; if exceeded, an error will be emitted.
  71. - `beforeRedirect` (default: `undefined`) – optionally change the request `options` on redirects, or abort the request by throwing an error.
  72. - `agents` (default: `undefined`) – sets the `agent` option per protocol, since HTTP and HTTPS use different agents. Example value: `{ http: new http.Agent(), https: new https.Agent() }`
  73. - `trackRedirects` (default: `false`) – whether to store the redirected response details into the `redirects` array on the response object.
  74. ### Advanced usage
  75. By default, `follow-redirects` will use the Node.js default implementations
  76. of [`http`](https://nodejs.org/api/http.html)
  77. and [`https`](https://nodejs.org/api/https.html).
  78. To enable features such as caching and/or intermediate request tracking,
  79. you might instead want to wrap `follow-redirects` around custom protocol implementations:
  80. ```javascript
  81. const { http, https } = require('follow-redirects').wrap({
  82. http: require('your-custom-http'),
  83. https: require('your-custom-https'),
  84. });
  85. ```
  86. Such custom protocols only need an implementation of the `request` method.
  87. ## Browser Usage
  88. Due to the way the browser works,
  89. the `http` and `https` browser equivalents perform redirects by default.
  90. By requiring `follow-redirects` this way:
  91. ```javascript
  92. const http = require('follow-redirects/http');
  93. const https = require('follow-redirects/https');
  94. ```
  95. you can easily tell webpack and friends to replace
  96. `follow-redirect` by the built-in versions:
  97. ```json
  98. {
  99. "follow-redirects/http" : "http",
  100. "follow-redirects/https" : "https"
  101. }
  102. ```
  103. ## Contributing
  104. Pull Requests are always welcome. Please [file an issue](https://github.com/follow-redirects/follow-redirects/issues)
  105. detailing your proposal before you invest your valuable time. Additional features and bug fixes should be accompanied
  106. by tests. You can run the test suite locally with a simple `npm test` command.
  107. ## Debug Logging
  108. `follow-redirects` uses the excellent [debug](https://www.npmjs.com/package/debug) for logging. To turn on logging
  109. set the environment variable `DEBUG=follow-redirects` for debug output from just this module. When running the test
  110. suite it is sometimes advantageous to set `DEBUG=*` to see output from the express server as well.
  111. ## Authors
  112. - [Ruben Verborgh](https://ruben.verborgh.org/)
  113. - [Olivier Lalonde](mailto:olalonde@gmail.com)
  114. - [James Talmage](mailto:james@talmage.io)
  115. ## License
  116. [MIT License](https://github.com/follow-redirects/follow-redirects/blob/master/LICENSE)