-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
174 lines (146 loc) · 4.32 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// DO NOT CHANGE!
//init app with express, util, body-parser, csv2json
var express = require('express');
var app = express();
var sys = require('util');
var path = require('path');
var bodyParser = require('body-parser');
var Converter = require("csvtojson").Converter;
//register body-parser to handle json from res / req
app.use(bodyParser.json());
//register public dir to serve static files (html, css, js)
app.use(express.static(path.join(__dirname, "public")));
// END DO NOT CHANGE!
function nocache(req, res, next) {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
}
/**************************************************************************
****************************** csv2json *********************************
**************************************************************************/
var data = [];
var converter = new Converter({});
converter.fromFile("./world_data.csv", function(err, result) {
data = result;
});
/**************************************************************************
********************** handle HTTP METHODS ***********************
**************************************************************************/
/**
* GET-Methods
*/
/**
* get all countries with properties
*/
app.get('/items', nocache, function(req, res) {
res.send(data);
});
/**
* get country with given id and properties
*/
app.get('/items/:id', nocache, function(req, res) {
var id = req.params.id;
var country = data.find(function(element) {
return element.id == id;
});
if (country !== undefined) {
res.send(country);
} else {
res.status(404).send('No such id {' + id + '} in database.');
}
});
/**
* get countries in defined range with properties
*/
app.get('/items/:id1/:id2', nocache, function(req, res) {
var id1 = +req.params.id1 <= +req.params.id2 ? +req.params.id1 : +req.params.id2;
var id2 = +req.params.id1 >= +req.params.id2 ? +req.params.id1 : +req.params.id2;
var result = [];
for (i = id1; i <= id2; i++) {
if (country = data.find(function(element) {
return element.id == i;
})) {
result.push(country);
}
}
if (result.length == 0) {
res.status(400).send('Range not possible.');
} else {
res.send(result);
}
});
/**
* returns all the properties of a data object
*/
app.get('/properties', nocache, function(req, res) {
if (data.length > 0) {
res.send(Object.keys(data[0]));
} else {
res.send('No data available');
}
});
/**
* returns the property with the given number if exists
*/
app.get('/properties/:num', nocache, function(req, res) {
var num = req.params.num;
if (data.length > 0) {
// #TODO: check if num in bounds of array (NaN ??)
var keys = Object.keys(data[0]);
if (keys[num] !== undefined) {
res.send(keys[num]);
} else {
res.send('No such property available.');
}
} else {
res.send('No data available');
}
});
/**
* POST-Methods
*/
app.post('/items', function(req, res) {
var name = req.body.name;
// maybe we should validate the input more
// #TODO: we could adjust the json to fit in the original pattern
data.push(req.body);
res.send('Added country ' + name + ' to list!');
});
/**
* DELETE-Methods
*/
/**
* deletes the last country
*/
app.delete('/items', function(req, res) {
if (data.length > 0) {
var popped = data.pop();
res.send('Deleted last country: ' + popped.name);
} else {
res.send('No data available.');
}
});
/**
* deletes the country with the given id
*/
app.delete('/items/:id', function(req, res) {
var id = req.params.id;
var index = data.findIndex(function(element) {
return element.id == id;
});
if (index >= 0) {
data.splice(index, 1);
res.send('Item {' + id + '} deleted successfully.');
} else {
res.status(404).send('No such id {' + id + '} in database');
}
});
// DO NOT CHANGE!
// bind server to port
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});