alexarank

A simple node package to get Alexa traffic rank for a domain or URL
Log | Files | Refs | README | LICENSE

commit 950fae8be55fdcfb888c8ae5ddf11f23ce066d25
parent 44481c5a4ee0baefd6516630c6c7a45448397e8c
Author: Jannis R <mail@jannisr.de>
Date:   Fri, 21 Oct 2016 21:01:48 +0200

request -> isomorphic-fetch

Diffstat:
Mlib/alexarank.js | 49++++++++++++++++++++++++-------------------------
Mpackage.json | 4++--
2 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/lib/alexarank.js b/lib/alexarank.js @@ -14,36 +14,35 @@ /* */ /*****************************************************************************/ -var request = require('request'); +var fetch = require('isomorphic-fetch'); var xml2js = require('xml2js'); module.exports = function(url, callback) { - request('https://data.alexa.com/data?cli=10&url=' + encodeURIComponent(url), function(error, response, body) { - if (error) { - callback(new Error('Cannot reach Alexa API')); - } else if (response.statusCode != 200) { - callback(new Error('Cannot fetch Alexa API Data')); - } else { - xml2js.parseString(body, { - normalizeTags: true, - explicitArray: false - }, function(error, result) { - if (error) { - callback(new Error('Cannot parse Alexa API Data')); - } else { - var alexa = {}; + fetch('https://data.alexa.com/data?cli=10&url=' + encodeURIComponent(url)) + .then((res) => res.text()) + .then((body) => { + xml2js.parseString(body, { + normalizeTags: true, + explicitArray: false + }, function(error, result) { + if (error) { + callback(new Error('Cannot parse Alexa API Data')); + } else { + var alexa = {}; - alexa.url = result.alexa.$.URL; - alexa.idn = result.alexa.$.IDN; + alexa.url = result.alexa.$.URL; + alexa.idn = result.alexa.$.IDN; - if (typeof result.alexa.sd != "undefined") { - alexa.rank = result.alexa.sd.popularity.$.TEXT; - alexa.reach = result.alexa.sd.reach.$.RANK; - } - - callback(null, alexa); + if (typeof result.alexa.sd != "undefined") { + alexa.rank = result.alexa.sd.popularity.$.TEXT; + alexa.reach = result.alexa.sd.reach.$.RANK; } - }); - } + + callback(null, alexa); + } + }); + }) + .catch((err) => { + callback(err); }); }; diff --git a/package.json b/package.json @@ -32,8 +32,8 @@ "url": "https://github.com/fcambus/alexarank/issues" }, "dependencies": { - "xml2js": "^0.4", - "request": "^2.75" + "isomorphic-fetch": "^2.2.1", + "xml2js": "^0.4" }, "devDependencies": { "mocha": "2.3.4",