-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·269 lines (234 loc) · 7.78 KB
/
index.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Dependencies
const express = require('express');
const path = require("path");
const PythonShell = require('python-shell');
const fs = require('fs');
const csvWriter = require("csv-write-stream");
const _ = require('lodash');
const bodyParser = require('body-parser');
const csv = require('csvtojson');
const jsonfile = require('jsonfile');
let app = express();
let writer = csvWriter({ sendHeaders: false });
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.set('port', (process.env.PORT || 7101))
// Add headers
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
// For Rendering HTML
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/dev/index.html'));
})
app.use(express.static(__dirname + '/dev'));
app.listen(app.get('port'), function () {
console.log("Node app is running at http://localhost:" + app.get('port'))
})
// POST endpoint for requesting trials
app.post('/trials', function (req, res, next) {
console.log("trials post request received");
let subjCode = req.body.subjCode;
let sessionId = req.body.sessionId;
console.log(req.body);
let trials = [];
fs.readdir('./trials', (err, filenames) => {
filenames = filenames.filter(filename =>
filename.startsWith("trials_drawing_")
);
let filename = filenames[Math.floor(Math.random()*filenames.length)];
csv({delimiter: ','})
.fromFile('./trials/'+filename)
.on('json',(jsonObj)=>{
trials.push(jsonObj);
})
.on('done',(error)=>{
trials.forEach((trial) => {
trial.file = filename;
});
// Parses to lines of questions to a shuffled array of questions
let questions = _.shuffle(fs.readFileSync('IRQ_questions.txt').toString().replace(/\r/g, '\n').split('\n')).filter((line) => {return line.replace(/ /g, '').length > 0 });
let ReadingQu1 = _.shuffle(fs.readFileSync('reading_questions_1.txt').toString().replace(/\r/g, '\n').split('\n').filter((line) => {return line.replace(/ /g, '').length > 0 }));
let ReadingQu2 = _.shuffle(fs.readFileSync('reading_questions_2.txt').toString().replace(/\r/g, '\n').split('\n').filter((line) => {return line.replace(/ /g, '').length > 0 }));
console.log(trials)
res.send({ success: true, trials: trials, questions, ReadingQu: [ReadingQu1, ReadingQu2] });
})
})
})
// POST endpoint for receiving trial responses
app.post('/data', function (req, res, next) {
console.log('data post request received');
// Create new data folder if does not exist
let response = req.body;
fs.access('./data', (err) => {
if (err && err.code === 'ENOENT') {
fs.mkdir('./data', () => {
next();
});
}
else next();
});
},
(req, res, next) => {
let response = req.body;
let path = 'data/' + response.workerId + '_data.json';
fs.access(path, (err) => {
if (err && err.code === 'ENOENT') {
jsonfile.writeFile(path, { trials: [] }, (err) => {
if (err) {
res.send({ success: false });
return next(err);
}
next();
})
}
else next();
})
},
(req, res, next) => {
// Write response to json
let response = req.body;
let path = 'data/' + response.workerId + '_data.json';
console.log(response);
jsonfile.readFile(path, (err, obj) => {
if (err) {
res.send({ success: false });
return next(err);
}
obj.trials.push(response);
jsonfile.writeFile(path, obj, (err) => {
if (err) {
res.send({ success: false });
return next(err);
}
res.send({ success: true });
})
})
});
// POST endpoint for receiving demographics responses
app.post('/demographics', function (req, res, next) {
let demographics = req.body;
console.log('demographics post request received');
console.log(demographics);
let path = 'demographics/' + demographics.subjCode + '_demographics.csv';
fs.access('./demographics', (err) => {
if (err && err.code === 'ENOENT') {
fs.mkdir('./demographics', () => {
next();
});
}
else next();
});
},
(req, res, next) => {
let demographics = req.body;
let path = 'demographics/' + demographics.subjCode + '_demographics.csv';
fs.access(path, (err) => {
if (err && err.code === 'ENOENT') {
jsonfile.writeFile(path, { trials: [] }, (err) => {
if (err) {
res.send({ success: false });
return next(err);
}
next();
})
}
else next();
})
}, (req, res, next) => {
// Parses the trial response data to csv
let demographics = req.body;
let path = 'demographics/' + demographics.subjCode + '_demographics.csv';
writer = csvWriter({ headers: ['subjCode', 'question', 'response'] });
writer.pipe(fs.createWriteStream(path, { flags: 'w' }));
demographics.responses.forEach((response) => {
writer.write(response);
});
writer.end();
res.send({ success: true });
});
// POST endpoint for receiving image responses
app.post('/image', function (req, res, next) {
console.log('image post request received');
// Create new data folder if does not exist
let response = req.body;
fs.access('./data', (err) => {
if (err && err.code === 'ENOENT') {
fs.mkdir('./data', () => {
next();
});
}
else next();
});
},
(req, res, next) => {
// Create new user folder if does not exist
let response = req.body;
fs.access('./data/' + response.workerId, (err) => {
if (err && err.code === 'ENOENT') {
fs.mkdir('./data/' + response.workerId, () => {
next();
});
}
else next();
});
},
(req, res, next) => {
// save as png
let response = req.body;
console.log(Object.keys(response))
var base64Data = response.drawing.replace(/^data:image\/png;base64,/, "");
fs.writeFile('./data/' + response.workerId+'/'+response.workerId+'_'+response.word_to_draw+'_'+response.trial_number+'.png', base64Data, 'base64', function (err) {
if (err) {
res.send({ success: false });
return next(err);
}
res.send({ success: true });
});
});
// POST endpoint for receiving trial responses
app.post('/IRQ', function (req, res) {
console.log('IRQ post request received');
// Parses the trial response data to csv
let IRQ = req.body;
console.log(IRQ);
if (!fs.existsSync('IRQ')) {
fs.mkdirSync('IRQ');
}
let path = 'IRQ/' + IRQ.subjCode + '_IRQ.csv';
writer = csvWriter({ headers: ['subjCode', 'question', 'response'] });
writer.pipe(fs.createWriteStream(path, { flags: 'w' }));
IRQ.responses.forEach((response) => {
console.log(response);
writer.write(response);
})
writer.end();
res.send({ success: true });
})
// POST endpoint for receiving trial responses
app.post('/ReadingQu', function (req, res) {
console.log('ReadingQu post request received');
// Parses the trial response data to csv
let ReadingQu = req.body;
console.log(ReadingQu);
if (!fs.existsSync('ReadingQu')) {
fs.mkdirSync('ReadingQu');
}
let path;
if (ReadingQu.batch === 1) {
path = 'ReadingQu/' + ReadingQu.subjCode + '_ReadingQu1.csv';
} else if (ReadingQu.batch === 2) {
path = 'ReadingQu/' + ReadingQu.subjCode + '_ReadingQu2.csv';
}
writer = csvWriter({ headers: ['subjCode', 'question', 'response'] });
writer.pipe(fs.createWriteStream(path, { flags: 'w' }));
ReadingQu.responses.forEach((response) => {
writer.write(response);
});
writer.end();
res.send({ success: true });
})