Na wstepie przepraszam za tytul ale nie wiem za bardzo jak to na pl przetlumaczyc.
Sprawa ma sie tak: Mam w node call do API MOZ. Moj kod wyglada tak:
function MozCall(callback) {
var mozCall = '';
// Set your expires times for several minutes into the future.
// An expires time excessively far in the future will not be honored by the Mozscape API.
// Divide the result of Date.now() by 1000 to make sure your result is in seconds.
var expires = Math.floor(Date.now() / 1000) + 300;
var accessId = 'key';
var secretKey = 'key';
// `bitFlagExampleValues` is a list of bitFlag values as strings that we'll
// loop over and sum together using helper function: `sumColumnValues`
var bitFlagExampleValues = [
'144115188075855872',
'68719476736',
'34359738368',
];
var sumColumnValues = function(bitFlagValues) {
return bitFlagValues.reduce(function(accu, bitFlag) {
var accuValBig = new bigJs(accu);
var bitFlagBig = new bigJs(bitFlag);
var bigSum = accuValBig.plus(bitFlagBig);
return bigSum.toString();
}, 0);
};
// 'cols' is the sum of the bit flags representing each field you want returned.
// Learn more here: https://moz.com/help/guides/moz-api/mozscape/api-reference/url-metrics
// returns "144115291155070976"
var cols = sumColumnValues(bitFlagExampleValues);
// Put each parameter on a new line.
var stringToSign = accessId + '\n' + expires;
//create the hmac hash and Base64-encode it.
var signature = crypto
.createHmac('sha1', secretKey)
.update(stringToSign)
.digest('base64');
//URL-encode the result of the above.
signature = encodeURIComponent(signature);
var postData = JSON.stringify([
website,
'www.apple.com',
'www.pizza.com',
]);
var options = {
hostname: 'lsapi.seomoz.com',
path:
'/linkscape/url-metrics/?Cols=' +
cols +
'&AccessID=' +
accessId +
'&Expires=' +
expires +
'&Signature=' +
signature,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length,
},
};
var responseData = '';
var req = http.request(options, function(response) {
response.setEncoding('utf8');
response.on('data', function(chunk) {
responseData += chunk;
});
response.on('end', function() {
console.log(responseData);
});
});
req.end()
}
// if (req.user.isPremium == false) {
let freeReportCalls = [MozCall];
// Free user - Single report
// let website = req.body.website0;
async.parallel(freeReportCalls, function(err, results) {
if (err) {
console.log(err);
} else {
console.log(results);
res.render('reports/report', {
title: 'Report',
// bw: JSON.parse(results[0]),
// ps: JSON.parse(results[0]),
// bw: results[1],
// al: results[2],
// moz: results[2],
user: req.user,
});
}
});
Jest to wlasciwie kod przykladowy z tego linku: https://github.com/seomoz/SEOmozAPISamples/blob/master/javascript/node/batching-urls-sample.js
Problem w tym ze za kazdym razem dostaje taki oto kod bledu:
events.js:183
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (_http_client.js:331:15)
at Socket.socketOnEnd (_http_client.js:423:23)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
Zgaduje ze cos robie zle w kodzie ale nie mam bladego pojecia co. Probowalem podac zly klucz ale nie dostalem zadnego innego bledu. Inne zapytania idace rownolegle dzialaja. Znalazlem ta dyskusje ale nie wiem na ile odnosi sie ona do mojego problemu: https://github.com/Automattic/knox/issues/116
Moj post na StackOverflow o tym samym problemie (Moze komus jakis komentarz albo odpowiedz cos podpowie): https://stackoverflow.com/questions/49541108/moz-api-call-times-out
Edit: Dodanie req.end() rozwiazalo problem, ale teraz dostaje kod 503, service temporarily unavailable. Ktos wie o co dokladnie chodzi?