我正在使用node来发出Moz API请求。这是我的代码:
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,
});
}
});
我从他们的github示例中获取了这些代码,可以在这里找到:https://github.com/seomoz/SEOmozAPISamples/blob/master/javascript/node/batching-urls-sample.js
但每次拨打电话都会收到此错误。
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)
由于event.js是一个软件包的一部分,我非常确定我做错了什么,但我不知道是什么。我尝试破解API密钥,但看起来它似乎并不想连接到Moz服务器。知道这里有什么问题吗?
编辑:我发现了这个讨论,但我不确定它是否与问题有关https://github.com/Automattic/knox/issues/116
Edit2:我添加了一个req.end()
来解决超时问题。现在我得到503, service temporarily unavailable
。知道他们正在谈论哪种服务?
答案 0 :(得分:0)
好的,最后添加req.end()
解决了超时问题。我将为我的新问题打开另一篇文章。