PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/follow-redirects/README.md

https://bitbucket.org/syamsulmj94/fyp-project-test
Markdown | 155 lines | 115 code | 40 blank | 0 comment | 0 complexity | 69909f6bdb398c818c4309ef3a613a9a MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, CC-BY-4.0, Unlicense, BSD-2-Clause, GPL-2.0, 0BSD, MIT, Apache-2.0, CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception
  1. ## Follow Redirects
  2. Drop-in replacement for Nodes `http` and `https` that automatically follows redirects.
  3. [![npm version](https://badge.fury.io/js/follow-redirects.svg)](https://www.npmjs.com/package/follow-redirects)
  4. [![Build Status](https://travis-ci.org/olalonde/follow-redirects.svg?branch=master)](https://travis-ci.org/olalonde/follow-redirects)
  5. [![Coverage Status](https://coveralls.io/repos/olalonde/follow-redirects/badge.svg?branch=master)](https://coveralls.io/r/olalonde/follow-redirects?branch=master)
  6. [![Dependency Status](https://david-dm.org/olalonde/follow-redirects.svg)](https://david-dm.org/olalonde/follow-redirects)
  7. [![devDependency Status](https://david-dm.org/olalonde/follow-redirects/dev-status.svg)](https://david-dm.org/olalonde/follow-redirects#info=devDependencies)
  8. [![NPM](https://nodei.co/npm/follow-redirects.png?downloads=true)](https://nodei.co/npm/follow-redirects/)
  9. `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)
  10. 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)
  11. modules, with the exception that they will seamlessly follow redirects.
  12. ```javascript
  13. var http = require('follow-redirects').http;
  14. var https = require('follow-redirects').https;
  15. http.get('http://bit.ly/900913', function (response) {
  16. response.on('data', function (chunk) {
  17. console.log(chunk);
  18. });
  19. }).on('error', function (err) {
  20. console.error(err);
  21. });
  22. ```
  23. You can inspect the final redirected URL through the `responseUrl` property on the `response`.
  24. If no redirection happened, `responseUrl` is the original request URL.
  25. ```javascript
  26. https.request({
  27. host: 'bitly.com',
  28. path: '/UHfDGO',
  29. }, function (response) {
  30. console.log(response.responseUrl);
  31. // 'http://duckduckgo.com/robots.txt'
  32. });
  33. ```
  34. ## Options
  35. ### Global options
  36. Global options are set directly on the `follow-redirects` module:
  37. ```javascript
  38. var followRedirects = require('follow-redirects');
  39. followRedirects.maxRedirects = 10;
  40. followRedirects.maxBodyLength = 20 * 1024 * 1024; // 20 MB
  41. ```
  42. The following global options are supported:
  43. - `maxRedirects` (default: `21`) sets the maximum number of allowed redirects; if exceeded, an error will be emitted.
  44. - `maxBodyLength` (default: 10MB) sets the maximum size of the request body; if exceeded, an error will be emitted.
  45. ### Per-request options
  46. Per-request options are set by passing an `options` object:
  47. ```javascript
  48. var url = require('url');
  49. var followRedirects = require('follow-redirects');
  50. var options = url.parse('http://bit.ly/900913');
  51. options.maxRedirects = 10;
  52. http.request(options);
  53. ```
  54. 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),
  55. the following per-request options are supported:
  56. - `followRedirects` (default: `true`) whether redirects should be followed.
  57. - `maxRedirects` (default: `21`) sets the maximum number of allowed redirects; if exceeded, an error will be emitted.
  58. - `maxBodyLength` (default: 10MB) sets the maximum size of the request body; if exceeded, an error will be emitted.
  59. - `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() }`
  60. ### Advanced usage
  61. By default, `follow-redirects` will use the Node.js default implementations
  62. of [`http`](https://nodejs.org/api/http.html)
  63. and [`https`](https://nodejs.org/api/https.html).
  64. To enable features such as caching and/or intermediate request tracking,
  65. you might instead want to wrap `follow-redirects` around custom protocol implementations:
  66. ```javascript
  67. var followRedirects = require('follow-redirects').wrap({
  68. http: require('your-custom-http'),
  69. https: require('your-custom-https'),
  70. });
  71. ```
  72. Such custom protocols only need an implementation of the `request` method.
  73. ## Browserify Usage
  74. Due to the way `XMLHttpRequest` works, the `browserify` versions of `http` and `https` already follow redirects.
  75. If you are *only* targeting the browser, then this library has little value for you. If you want to write cross
  76. platform code for node and the browser, `follow-redirects` provides a great solution for making the native node
  77. modules behave the same as they do in browserified builds in the browser. To avoid bundling unnecessary code
  78. you should tell browserify to swap out `follow-redirects` with the standard modules when bundling.
  79. To make this easier, you need to change how you require the modules:
  80. ```javascript
  81. var http = require('follow-redirects/http');
  82. var https = require('follow-redirects/https');
  83. ```
  84. You can then replace `follow-redirects` in your browserify configuration like so:
  85. ```javascript
  86. "browser": {
  87. "follow-redirects/http" : "http",
  88. "follow-redirects/https" : "https"
  89. }
  90. ```
  91. The `browserify-http` module has not kept pace with node development, and no long behaves identically to the native
  92. module when running in the browser. If you are experiencing problems, you may want to check out
  93. [browserify-http-2](https://www.npmjs.com/package/http-browserify-2). It is more actively maintained and
  94. attempts to address a few of the shortcomings of `browserify-http`. In that case, your browserify config should
  95. look something like this:
  96. ```javascript
  97. "browser": {
  98. "follow-redirects/http" : "browserify-http-2/http",
  99. "follow-redirects/https" : "browserify-http-2/https"
  100. }
  101. ```
  102. ## Contributing
  103. Pull Requests are always welcome. Please [file an issue](https://github.com/olalonde/follow-redirects/issues)
  104. detailing your proposal before you invest your valuable time. Additional features and bug fixes should be accompanied
  105. by tests. You can run the test suite locally with a simple `npm test` command.
  106. ## Debug Logging
  107. `follow-redirects` uses the excellent [debug](https://www.npmjs.com/package/debug) for logging. To turn on logging
  108. set the environment variable `DEBUG=follow-redirects` for debug output from just this module. When running the test
  109. suite it is sometimes advantageous to set `DEBUG=*` to see output from the express server as well.
  110. ## Authors
  111. - Olivier Lalonde (olalonde@gmail.com)
  112. - James Talmage (james@talmage.io)
  113. - [Ruben Verborgh](https://ruben.verborgh.org/)
  114. ## License
  115. MIT: [http://olalonde.mit-license.org](http://olalonde.mit-license.org)