1- var http = require ( 'https' ) ;
2- var urls = [
3- 'https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=barack%20obama' ,
4- 'https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=technology'
5- ] ;
6- var allResults = [ ] ;
7- var responded = 0 ;
8- function collectResponse ( res ) {
9- var response = { } ;
10- res . setEncoding ( 'utf8' ) ;
11- res . on ( 'data' , function ( d ) {
12- response . body = d ;
13- } ) ;
1+ var http = require ( 'http' ) ;
2+ function Restapi ( ) {
3+ this . curlCall = function ( req , res ) {
4+ var search = req . query . search ;
5+ var url = 'http://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=' + search ;
6+ http . get ( url , this . collectData ) ;
7+ }
8+ this . collectData = function ( req , res ) {
149
15- res . on ( 'end' , function ( ) {
16- //console.log(responseBody);
17- // var response = JSON.parse(responseBody);
18- // allResults = allResults.concat(response.results);
19- console . log ( response . body . signedRedirectUrl ) ;
20- // for(var i=0;i<=allResults.length;i++){
21- // console.log(allResults.content[i]);
22- // }
23- // response.forEach(function(result){
24- // console.log(result);
25- // });
26- // console.log('I have %d results for', response.results.length, res.req.path);
27- // responded += 1;
28- // if (responded == urls.length) {
29- // console.log('All responses ended. Number of total results:', allResults.length);
30- // }
31- } ) ;
10+ res . setEncoding ( 'utf8' ) ;
11+ res . on ( 'data' , function ( d ) {
12+ responseBody = d ;
13+ } ) ;
14+ res . on ( 'end' , function ( request , response ) {
15+ var responseEnd = JSON . parse ( responseBody ) ;
16+ console . log ( responseEnd ) ;
17+ return response . render ( 'index' , {
18+ title : 'Index Page'
19+ } ) ;
20+ } ) ;
21+ }
3222}
33- urls . forEach ( function ( url ) {
34- http . get ( url , collectResponse ) ;
35- } ) ;
23+ module . exports = Restapi ;
0 commit comments