-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (79 loc) · 2.12 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
var express = require('express');
var fs = require('fs');
var app = express();
var staticMiddleware;
var buildDir;
app.configure('development', function(){
buildDir = '/build';
staticMiddleware = express.static(__dirname + buildDir);
console.log('Configured server for DEV mode');
});
app.configure('production', function() {
buildDir = '/bin';
staticMiddleware = express.static(__dirname + buildDir);
console.log('Configured server for PROD mode');
});
app.use(express.bodyParser());
app.use(staticMiddleware);
app.use(function(err, req, res, next) {
res.send(500, 'Opps, something broke!');
});
app.get('/', function(req, res) {
fs.readFile(__dirname + buildDir + '/index.html', 'utf8', function(err, text) {
if (err) {
res.send(500, 'Could not find index.html!');
}
else {
res.send(text);
}
});
})
// handle file upload
app.post('/upload', function(req, res) {
res.setHeader('Content-Type', 'text/html');
if (req.files.length == 0 || req.files.file.size == 0)
res.send(JSON.stringify({ success: false, msg: 'No file uploaded.' }));
else {
var file = req.files.file;
var obj = {
success: true,
msg: 'Upload successful',
habits: []
};
var habits = {};
// read data into json
fs.readFile(file.path, 'utf8', function(err, data) {
if (err) {
fs.unlink(file.path, function(err) {
if (err) throw err;
});
throw err;
}
var lines = data.split(/\n/);
// skip the headers
for (var i = 1; i < lines.length; i++) {
if (lines[i].trim() !== '') {
var fields = lines[i].split(/,(?=([^\"]*\"[^\"]*\")*[^\"]*$)/);
var name = fields[2];
if (habits[name] === undefined) {
habits[name] = [];
}
habits[name].push({ date: fields[4], count: fields[8] });
}
}
for (h in habits) {
obj.habits.push({ name: h, checkIns: habits[h] });
}
fs.unlink(file.path, function(err) {
if (err) throw err;
});
res.send(JSON.stringify(obj));
});
}
});
app.use(function(req, res, next) {
res.send(404, 'Sorry cant find that!');
});
var port = process.env.PORT || 3000;
app.listen(port);
console.log('Listening on port ' + port);