forked from winsonfzyang/jsPsychTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (41 loc) · 1.42 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
// --- LOAD MODULES
var express = require("express"),
mymods = require("./scripts/mymods.js"),
body_parser = require("body-parser");
var savedropbox = mymods.saveDropbox;
var json2csv = mymods.json2csv;
// --- INSTANTIATE THE APP
var app = express();
// --- STATIC MIDDLEWARE
app.use(express.static(__dirname + '/public'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/scripts", express.static(__dirname + '/scripts'));
app.use(body_parser.json());
// --- VIEW LOCATIONS. SET UP SERVING STATIC HTML
app.set("views", __dirname + "/public/views");
app.engine("html", require("ejs").renderFile);
app.set("view engine", "html");
// --- ROUTING
app.get("/", function (request, response) {
response.render("index.html")
});
app.get("/finish", function(request, response) {
response.render("finish.html");
});
app.post('/experiment-data', function(request, response) {
// convert json to csv
DATA_CSV = json2csv(request.body);
// Get filename from data
var rows = DATA_CSV.split("\n");
ID_DATE_index = rows[0].split(",").indexOf('"ID_DATE"');
ID_DATE = rows[1].split(",")[ID_DATE_index];
ID_DATE = ID_DATE.replace(/"/g, "");
filename = ID_DATE + ".csv";
savedropbox(DATA_CSV, filename);
response.end();
// console.log(DATA_CSV)
});
// --- START THE SERVER
var server = app.listen(process.env.PORT, function(){
console.log("listening to port %d", server.address().port);
});