Skip to content

Commit fb0a543

Browse files
author
Vivek G.S
committed
cleaning the code
1 parent fee840a commit fb0a543

File tree

6 files changed

+43
-47
lines changed

6 files changed

+43
-47
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.project
2+
*.sublime-project
3+
*.sublime-workspace
4+
*.tmproj
5+
*.bak
6+
*.swp
7+
*~.nib
8+
node_modules/

app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var express = require('express')
88
app.set('views', __dirname + '/public');
99

1010
app.use(express.bodyParser());
11-
11+
app.use(app.routes);
1212
routes(app);
1313

1414
app.listen(3001);

dummy.js

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
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;

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</head>
77
<body>
88
<div id='content' >
9-
<form id='send-form' method="get" action="/getresult">
9+
<form id='send-form' method="get" action="/getresult/">
1010
<input id='search' type='text' value='' name="search"/>
1111
<input id='send' type='submit' value='Search'/>
1212
</form>

routes/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
var Restapi = require('./restapi')
1+
var Restapi = require('./rest/restapi')
22
module.exports = exports = function(app) {
33

4-
var restapi = new RestAPI(app);
4+
var restapi = new Restapi(app);
55

66
app.get('/', function(req,res){
77
return res.render('index', {
88
title: 'Index Page'
99
});
1010
});
11-
12-
app.get('/getresult/:search', restapi.curlCall);
11+
app.get('/getresult/', restapi.curlCall);
1312
}

routes/rest/restapi.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
var https = require('https');
2-
function RestAPI () {
1+
var http = require('http');
2+
function Restapi () {
33
this.curlCall = function(req,res){
44
var search = req.query.search;
5-
var url = 'https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q='+search;
6-
console.log(url);
7-
https.get(url,function(req,res){
8-
console.log(res);
5+
var url = 'http://ajax.googleapis.com/ajax/services/search/news?v=1.0&q='+search;
6+
http.get(url,function(hreq,hres){
7+
//hres.setEncoding('utf8');
8+
//var resNews = JSON.parse(hres);
9+
//console.log(resNews);
910
return res.render('index', {
1011
title: 'Search Page',
11-
data:res
12+
data:hres
1213
});
1314
});
1415
}
1516
}
16-
module.exports = RestAPI;
17+
module.exports = Restapi;

0 commit comments

Comments
 (0)