-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
88 lines (80 loc) · 2.37 KB
/
server.js
File metadata and controls
88 lines (80 loc) · 2.37 KB
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
var express = require('express');
var app = express();
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'GET');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
app.use(allowCrossDomain);
app.use(express.static(__dirname + '/public'));
app.get('/csv_download', function(req, res){
var url = require('url');
var url_parts = url.parse(req.url, true);
var query = url_parts.query;
var api_url = "http://localhost:8003/todmorden";
var request = require('request');
var get_params = {
'from': query.from,
'to': query.to,
};
// "All" is a path parameter whereas an individual
// category is a query parameter.
if (query.category == 'all') {
api_url += '/all';
} else {
get_params.category = query.category;
}
request.get({url: api_url, qs:get_params}, function (error, api_res, body) {
if (!error && api_res.statusCode == 200) {
var sensor_name;
try {
category = query.category.replace(/ /g, '-');
} catch (e) {
category = 'UNKNOWN';
}
res.setHeader('Content-disposition', 'attachment; filename=todmorden-'+category+'.csv');
res.setHeader('Content-type', 'text/csv');
res.charset = 'UTF-8';
var csv = convert_to_csv(body);
res.write(csv);
res.end();
} else {
console.log(error);
}
});
});
function convert_to_csv(body) {
// If it won't parse (usually because it is an error message),
// just write the body so at least the user has something to debug
try {
var json = JSON.parse(body);
} catch (e) {
return body;
}
var csv = "category, time, value\n";
for (var i = 0; i < json.length; i++){
var row = json[i];
var value = row.value;
// round to 2 d.p. if it is a number
// but if it is a string leave it alone
if (typeof value === 'number') {
value = value.toFixed(2);
}
csv += row.category+", "+date_format(row.time)+", "+value+"\n";
}
return csv;
}
function date_format(timestamp) {
var date = new Date(timestamp);
// The string we get from the server is UTC.
return date.toLocaleString("en-GB").substring(4, 24);
}
var port;
if (typeof process.argv[2] == 'undefined') {
port = 80;
} else {
port = process.argv[2];
}
app.listen(port);
console.log("Aqua App Server running on "+port);