forked from xiaozhi2/webgazertutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (77 loc) · 2.4 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
/// load modules
var express = require("express");
// instanitate the app
var app = express();
const subjects = {};
const starttime = Date.now();
//const BoxSDK = require("box-node-sdk");
//const fs = require('fs');
const Dropbox = require("dropbox").Dropbox;
const fetch = require("node-fetch");
const body_parser = require("body-parser");
const dbx = new Dropbox({
accessToken: 'YOURACCESSTOKEN',
fetch
});
saveDropbox = function (content, filename, foldername) {
return dbx.filesGetMetadata({
path: "/" + foldername,
}).catch(err => {
// console.log(err['error']['path'])
if (err.error.error.path['.tag'] == 'not_found') {
return dbx.filesCreateFolder({
path: "/" + foldername,
autorename: false,
});
} else {
throw err;
}
}).then(() => {
return dbx.filesUpload({
path: "/" + foldername + "/" + filename,
contents: content
});
});
};
saveDropboxSingleFile = function (content, filename) {
return dbx.filesUpload({
path: "/" + filename,
contents: content,
autorename: false,
mode: 'overwrite'
});
};
app.set('port', (process.env.PORT || 2500));
// static
app.use(express.static(__dirname + '/public'));
app.use(body_parser.json({ limit: "50mb" }));
app.get("/", function (request, response) {
response.render("index.html");
})
app.post("/data", function (request, response) {
//convert json to csv
request.setTimeout(0);
// DATA_CSV = json2csv(request.body);
data = request.body;
id = data[0].subject;
// id = row[1].split(",")[Id_index];
id = id.replace(/'/g, "");
var currentdate = new Date();
filename = Number(currentdate) + ".json";
foldername = id;
data = JSON.stringify(data);
saveDropbox(data, filename, foldername).catch(err => console.log(err))
}
);
app.post("/subject-status", function (request, response) {
subject_id = request.body.subject_id;
status = request.body.status;
subjects[subject_id] = status;
saveDropboxSingleFile(JSON.stringify(subjects), `subject_status_${starttime}.json`)
.then(() => console.log(`subjuct status recorded: ${subject_id},${status}`))
.catch(err => console.log(err));
});
//start the server
app.listen(app.get('port'), function () {
console.log("listening to port :",(process.env.PORT || 2500));
});