-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (86 loc) · 2.57 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Module dependencies.
*/
var express = require('express')
, request = require('request')
, async = require('async')
, http = require('http');
//
var app = express();
// all environments
app.set('port', process.env.PORT || 3030);
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.bodyParser());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser() );
app.use(express.session({ secret: 'some secret' }));
app.use(app.router);
app.use(function(req, res) {
return res.status(404).json('404 Not found!');
});
app.use(function(err, req, res, next) {
res.status(err.status || 500);
console.log('Error : ' + err);
return res.json({error: err});
});
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/characters', function(req, resp){
console.log("express server people api"+ req.query)
console.log(req.query.sort)
var i=1, respDATA = [];
var getResult = function(){
console.log("i-------------------1 :"+i)
request({
method :'GET',
url : 'http://swapi.co/api/people',
qs : {
"page": i
}
},
function(err,res,body){
console.log("i-------------------2 :"+i)
console.log(err, res.statusCode)
if(err){
throw(err)
}
if(res.statusCode >= 200 && res.statusCode < 400) {
if(typeof(body) == 'string'){
body = JSON.parse(body);
}
respDATA = respDATA.concat(body.results)
console.log("i-------------------3 :"+ respDATA.length)
if (++i <=5 && body.count > i*10){
process.nextTick(function() { getResult();})
}else{
// sort by height or mass
if (req.query.sort == 'height' || req.query.sort == 'mass'){
respDATA = respDATA.sort(function(a, b) {
return parseFloat(a[req.query.sort]) - parseFloat(b[req.query.sort]);
});
}
// sort by name
if (req.query.sort == 'name'){
respDATA = respDATA.sort(function(a,b) {
if ( a.name < b.name )
return -1;
if ( a.name > b.name )
return 1;
return 0;
});
}
return resp.json(respDATA)
}
}
})
}
// calling getResult90 for firs time
getResult();
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});