-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
144 lines (125 loc) · 3.7 KB
/
server.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// load the things we need
var express = require('express');
var request = require('request');
const promise = require('bluebird');
var app = express();
// set the view engine to ejs
app.set('view engine', 'ejs');
// use res.render to load up an ejs view file
// index page
app.get('/character/:name', function(req, res) {
var name = req.params.name;
//var count = 1;
var found = false;
for (var i = 1; i <= 10 && !found; i++) {
request('http://swapi.co/api/people?page='+i, function (error, response, body) {
if (!error && response.statusCode == 200) {
var result = JSON.parse(body);
result.results.forEach(function(data){
var breakName = data.name.split(' ')[0];
if(breakName.toLowerCase() == name.toLowerCase())
{
found = true;
console.log("Print this", breakName);
res.render('pages/character', data);
}
});
}
});
}
});
app.get('/planetresidents', function(req, res) {
var page = req.query.page;
if(Number(page) && Number(page)<7 && Number(page)>0)
{
var jsonToReturn = {};
var characters = [];
request('http://swapi.co/api/planets?page='+page, function (error, response, body) {
if(error) console.log(error);
if (!error && response.statusCode == 200) {
var result = JSON.parse(body);
result.results.forEach(function(data, idx, array){
//console.log(data);
jsonToReturn[data.name] = [];
return new Promise(function(resolve, reject){
data.residents.forEach(function(resident)
{
request(resident, function (error2, response2, body2) {
if(error) console.log(error);
var result2 = JSON.parse(body2);
jsonToReturn[data.name].push(result2.name);
resolve(jsonToReturn);
})
})
}).then(()=>{
if (idx === array.length - 1){
console.log("Last callback call at index " + idx + " with value ");
console.log(jsonToReturn);
//res.render('pages/planets', {json: jsonToReturn});
res.json(jsonToReturn);
return;
}
})
});
}
})
}
else
{
res.json({success: false, message: "Enter correct parameter for page. (0 > Page >= 6) "});
}
});
app.get('/characters', function(req, res) {
var sort = req.query.sort;
try{
sort = sort.toLowerCase();
}
catch (err)
{
console.log(err);
}
console.log(sort);
var jsonToReturn = [];
for (var i = 1; i < 6 && jsonToReturn.length < 50; i++) {
request('http://swapi.co/api/people?page='+i, function (error, response, body) {
if(error) console.log(error);
if (!error && response.statusCode == 200) {
var result = JSON.parse(body);
result.results.forEach(function(data){
if(data)
jsonToReturn.push(data);
if(jsonToReturn.length == 50)
{
if(sort == "name")
{
jsonToReturn.sort(function(a,b) {return (a[sort] > b[sort]) ? 1 : ((b[sort] > a[sort]) ? -1 : 0);} );
//res.render('pages/characters', {json: jsonToReturn});
res.json({success: true, count: jsonToReturn.length ,people: jsonToReturn});
return;
}
else if(sort == "height" || sort == "mass")
{
jsonToReturn.sort(function(a, b) {
return a[sort] - b[sort];
});
//res.render('pages/characters', {json: jsonToReturn});
res.json({success: true, count: jsonToReturn.length ,people: jsonToReturn});
return;
}
else
{
res.json({success: true, message: 'Sorting parameter is Invalid or Not Defined, This list is not Sorted.', count: jsonToReturn.length ,people: jsonToReturn});
return;
}
}
else
{
console.log(jsonToReturn.length);
}
});
}
});
}
});
app.listen(process.env.PORT || 3000);
console.log('8080 is the magic port');