Caravan
throttle REST request client.
Installation
Via npm
$ npm install caravan --save
var caravan= console //function
Via bower
$ bower install caravan --save
Via rawgit.com(the simple way)
API
caravan(urls,options) -> Promise(responses)
Do throttle request to urls
. return the bodies and errors.
It is handled one by one, but can change at options.concurrency
.
var urls= 'http://romanize.berabou.me/foo' 'http://romanize.berabou.me/bar' 'http://romanize.berabou.me/baz' 'http://localhost:404/notfound' // do serial (slowly)//[// 'foo',// 'bar',// 'baz',// {// [Error: connect ECONNREFUSED]// code: 'ECONNREFUSED',// errno: 'ECONNREFUSED',// syscall: 'connect'// }//] // do parallel (quickly) // do graceful (wait a ms)
Or, usage follows.
caravancaravan caravancaravancaravan
progress notifying
Promise will have a .progress
. the registered callback will be Invoked every time the request is fulfilled or rejected.
var urls= 'http://romanize.berabou.me/foo' 'http://romanize.berabou.me/bar' 'http://romanize.berabou.me/baz' 'http://localhost:404/notfound' // progress: 1 / 4// foo// progress: 2 / 4// bar// progress: 3 / 4// baz// progress: 4 / 4// { [Error: connect ECONNREFUSED 127.0.0.1:404]// code: 'ECONNREFUSED',// errno: 'ECONNREFUSED',// syscall: 'connect',// address: '127.0.0.1',// port: 404,// response: undefined }// done
Customize request
If passing an object to the urls
, can switch the verb, and send a header and data.
url/uri
: string
method
: string (default:'GET'
)
headers
: object (default:null
)
data
: object (default:null
)
It uses as an argument of superagent
Caravan options
delay
: number (default:0
)
Specify the delay a millsecond for next request.
var urls= 'http://example.com/' 'http://example.com/' console// deferred: 1s
concurrency
: number (default:1
)
Specify the number of throttle requests.
var urls= 'http://localhost/ping/1s' 'http://localhost/ping/2s' 'http://localhost/ping/3s' console// concurrency is 1: 6s console// concurrency is 3: 3s
raw
: bool (default:false
)
if true, response contains detailed information such as headers, statuscode...