Check if a HTTP response is successful.
Accepts a response object, with a
Optionally it takse a callback as second argument, which is called with an error object only if the response was non-successful. The error has
npm install is-ok
Usage
Used together with request or the built-inhttp
module.Accepts a response object, with a
statusCode
property, as first argument and returns true
if the status code was within the 2xx
range, otherwise it returns false
.Optionally it takse a callback as second argument, which is called with an error object only if the response was non-successful. The error has
statusCode
, headers
and body
properties set, copied from the response.var request = require('request');
var isOk = require('is-ok');
var fetch = function(url, callback) {
request.get(url, function(err, response, body) {
if(err) return callback(err);
if(!isOk(response, callback)) return;
callback(null, body);
});
}