版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # simple-get [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
  2. [travis-image]: https://img.shields.io/travis/feross/simple-get/master.svg
  3. [travis-url]: https://travis-ci.org/feross/simple-get
  4. [npm-image]: https://img.shields.io/npm/v/simple-get.svg
  5. [npm-url]: https://npmjs.org/package/simple-get
  6. [downloads-image]: https://img.shields.io/npm/dm/simple-get.svg
  7. [downloads-url]: https://npmjs.org/package/simple-get
  8. [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
  9. [standard-url]: https://standardjs.com
  10. ### Simplest way to make http get requests
  11. ## features
  12. This module is the lightest possible wrapper on top of node.js `http`, but supporting these essential features:
  13. - follows redirects
  14. - automatically handles gzip/deflate responses
  15. - supports HTTPS
  16. - supports specifying a timeout
  17. - supports convenience `url` key so there's no need to use `url.parse` on the url when specifying options
  18. - composes well with npm packages for features like cookies, proxies, form data, & OAuth
  19. All this in < 100 lines of code.
  20. ## install
  21. ```
  22. npm install simple-get
  23. ```
  24. ## usage
  25. Note, all these examples also work in the browser with [browserify](http://browserify.org/).
  26. ### simple GET request
  27. Doesn't get easier than this:
  28. ```js
  29. const get = require('simple-get')
  30. get('http://example.com', function (err, res) {
  31. if (err) throw err
  32. console.log(res.statusCode) // 200
  33. res.pipe(process.stdout) // `res` is a stream
  34. })
  35. ```
  36. ### even simpler GET request
  37. If you just want the data, and don't want to deal with streams:
  38. ```js
  39. const get = require('simple-get')
  40. get.concat('http://example.com', function (err, res, data) {
  41. if (err) throw err
  42. console.log(res.statusCode) // 200
  43. console.log(data) // Buffer('this is the server response')
  44. })
  45. ```
  46. ### POST, PUT, PATCH, HEAD, DELETE support
  47. For `POST`, call `get.post` or use option `{ method: 'POST' }`.
  48. ```js
  49. const get = require('simple-get')
  50. const opts = {
  51. url: 'http://example.com',
  52. body: 'this is the POST body'
  53. }
  54. get.post(opts, function (err, res) {
  55. if (err) throw err
  56. res.pipe(process.stdout) // `res` is a stream
  57. })
  58. ```
  59. #### A more complex example:
  60. ```js
  61. const get = require('simple-get')
  62. get({
  63. url: 'http://example.com',
  64. method: 'POST',
  65. body: 'this is the POST body',
  66. // simple-get accepts all options that node.js `http` accepts
  67. // See: http://nodejs.org/api/http.html#http_http_request_options_callback
  68. headers: {
  69. 'user-agent': 'my cool app'
  70. }
  71. }, function (err, res) {
  72. if (err) throw err
  73. // All properties/methods from http.IncomingResponse are available,
  74. // even if a gunzip/inflate transform stream was returned.
  75. // See: http://nodejs.org/api/http.html#http_http_incomingmessage
  76. res.setTimeout(10000)
  77. console.log(res.headers)
  78. res.on('data', function (chunk) {
  79. // `chunk` is the decoded response, after it's been gunzipped or inflated
  80. // (if applicable)
  81. console.log('got a chunk of the response: ' + chunk)
  82. }))
  83. })
  84. ```
  85. ### JSON
  86. You can serialize/deserialize request and response with JSON:
  87. ```js
  88. const get = require('simple-get')
  89. const opts = {
  90. method: 'POST',
  91. url: 'http://example.com',
  92. body: {
  93. key: 'value'
  94. },
  95. json: true
  96. }
  97. get.concat(opts, function (err, res, data) {
  98. if (err) throw err
  99. console.log(data.key) // `data` is an object
  100. })
  101. ```
  102. ### Timeout
  103. You can set a timeout (in milliseconds) on the request with the `timeout` option.
  104. If the request takes longer than `timeout` to complete, then the entire request
  105. will fail with an `Error`.
  106. ```js
  107. const get = require('simple-get')
  108. const opts = {
  109. url: 'http://example.com',
  110. timeout: 2000 // 2 second timeout
  111. }
  112. get(opts, function (err, res) {})
  113. ```
  114. ### One Quick Tip
  115. It's a good idea to set the `'user-agent'` header so the provider can more easily
  116. see how their resource is used.
  117. ```js
  118. const get = require('simple-get')
  119. const pkg = require('./package.json')
  120. get('http://example.com', {
  121. headers: {
  122. 'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`
  123. }
  124. })
  125. ```
  126. ### Proxies
  127. You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with the
  128. `agent` option to work with proxies:
  129. ```js
  130. const get = require('simple-get')
  131. const tunnel = require('tunnel')
  132. const opts = {
  133. url: 'http://example.com',
  134. agent: tunnel.httpOverHttp({
  135. proxy: {
  136. host: 'localhost'
  137. }
  138. })
  139. }
  140. get(opts, function (err, res) {})
  141. ```
  142. ### Cookies
  143. You can use the [`cookie`](https://github.com/jshttp/cookie) module to include
  144. cookies in a request:
  145. ```js
  146. const get = require('simple-get')
  147. const cookie = require('cookie')
  148. const opts = {
  149. url: 'http://example.com',
  150. headers: {
  151. cookie: cookie.serialize('foo', 'bar')
  152. }
  153. }
  154. get(opts, function (err, res) {})
  155. ```
  156. ### Form data
  157. You can use the [`form-data`](https://github.com/form-data/form-data) module to
  158. create POST request with form data:
  159. ```js
  160. const fs = require('fs')
  161. const get = require('simple-get')
  162. const FormData = require('form-data')
  163. const form = new FormData()
  164. form.append('my_file', fs.createReadStream('/foo/bar.jpg'))
  165. const opts = {
  166. url: 'http://example.com',
  167. body: form
  168. }
  169. get.post(opts, function (err, res) {})
  170. ```
  171. #### Or, include `application/x-www-form-urlencoded` form data manually:
  172. ```js
  173. const get = require('simple-get')
  174. const opts = {
  175. url: 'http://example.com',
  176. form: {
  177. key: 'value'
  178. }
  179. }
  180. get.post(opts, function (err, res) {})
  181. ```
  182. ### Specifically disallowing redirects
  183. ```js
  184. const get = require('simple-get')
  185. const opts = {
  186. url: 'http://example.com/will-redirect-elsewhere',
  187. followRedirects: false
  188. }
  189. // res.statusCode will be 301, no error thrown
  190. get(opts, function (err, res) {})
  191. ```
  192. ### OAuth
  193. You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) module to create
  194. a signed OAuth request:
  195. ```js
  196. const get = require('simple-get')
  197. const crypto = require('crypto')
  198. const OAuth = require('oauth-1.0a')
  199. const oauth = OAuth({
  200. consumer: {
  201. key: process.env.CONSUMER_KEY,
  202. secret: process.env.CONSUMER_SECRET
  203. },
  204. signature_method: 'HMAC-SHA1',
  205. hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
  206. })
  207. const token = {
  208. key: process.env.ACCESS_TOKEN,
  209. secret: process.env.ACCESS_TOKEN_SECRET
  210. }
  211. const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'
  212. const opts = {
  213. url: url,
  214. headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),
  215. json: true
  216. }
  217. get(opts, function (err, res) {})
  218. ```
  219. ### Throttle requests
  220. You can use [limiter](https://github.com/jhurliman/node-rate-limiter) to throttle requests. This is useful when calling an API that is rate limited.
  221. ```js
  222. const simpleGet = require('simple-get')
  223. const RateLimiter = require('limiter').RateLimiter
  224. const limiter = new RateLimiter(1, 'second')
  225. const get = (opts, cb) => limiter.removeTokens(1, () => simpleGet(opts, cb))
  226. get.concat = (opts, cb) => limiter.removeTokens(1, () => simpleGet.concat(opts, cb))
  227. var opts = {
  228. url: 'http://example.com'
  229. }
  230. get.concat(opts, processResult)
  231. get.concat(opts, processResult)
  232. function processResult (err, res, data) {
  233. if (err) throw err
  234. console.log(data.toString())
  235. }
  236. ```
  237. ## license
  238. MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).