I need in cycle create http.get request to google api|cache service. With nodejs i try do it
for(var i = 41923,postID=i; i <= 41925; i++) {
http.get(options, function(res) {
res.setEncoding('utf8');
res.on('data', function(data) {
responseBody += data;
}).on('end', function() {
if(getPostCacheUrl) {
var json = JSON.parse(responseBody);
json.items = json.items || [];
var cacheUrl = getPostCacheUrl(json);
parseCacheUrl(cacheUrl);
}
}).on('error', function(error) {
console.log('ERROR' + error.message);
});
});
}
But i catch error:
undefined:1
: 200}{"response
^
SyntaxError: Unexpected token {
at Object.parse (native)
at IncomingMessage.<anonymous> (/Users/macbookpro/Sites/googlecache/title.js:30:41)
at IncomingMessage.emit (events.js:88:20)
at HTTPParser.onMessageComplete (http.js:137:23)
at Socket.ondata (http.js:1137:24)
at TCP.onread (net.js:354:27)
First of all it looks like you are not getting any data… in line
there should be a handler from which you take response and parse it, eg:
Add console.log(responseBody) to check if it’s an object, I don’t remember what is passed in here.
I will not ask why you need to request so many data from google (watch out for the ban or anti spam actions) 😉
The other problem from what I can see here may be asynchronous requests. Please try below and let me know if it helped. It will wait for finish of request before sending next one. I’m guessing that is the problem.
The example can be checked here http://jsfiddle.net/Pu3cr/2/ but it will not work because of obvious reasons. I hope that it will help you with your project.
Please let me know if you need anything more.
Best,
Marek