-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
871 lines (759 loc) · 32.4 KB
/
Copy pathserver.js
File metadata and controls
871 lines (759 loc) · 32.4 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid'); // Import the UUID library
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const session = require('express-session');
const lockfile = require('proper-lockfile');
require('dotenv').config();
const axios = require('axios');
const RECAPTCHA_SECRET_KEY = process.env.RECAPTCHA_SECRET_KEY;
const app = express();
const port = 3000;
// Jin add this
app.get('/favicon.ico', (req, res) => {
res.status(204).end(); // Send a "No Content" response
});
app.get('/recaptcha_config.js', (req, res) => {
res.type('application/javascript').send(
`window.RECAPTCHA_SITE_KEY = ${JSON.stringify(process.env.RECAPTCHA_SITE_KEY)};`
);
});
// List of active experiments for this slimmed repository
const validExperiments = ['FIT5item'];
const experimentNameToFolder = {
FIT5item: 'FIT5item_Exp',
};
function getExperimentFolder(experimentName) {
return experimentNameToFolder[experimentName] || `${experimentName}_Exp`;
}
// Map public slugs to internal experiment names to avoid exposing study names in URLs
const slugToExperimentName = {};
const experimentNameToSlug = {};
validExperiments.forEach((experimentName) => {
let publicSlug = experimentName;
try {
const cfgPath = path.join(__dirname, 'Experiments', getExperimentFolder(experimentName), 'config.json');
if (fs.existsSync(cfgPath)) {
const cfg = JSON.parse(fs.readFileSync(cfgPath, 'utf8'));
if (cfg && typeof cfg.publicSlug === 'string' && cfg.publicSlug.trim()) {
publicSlug = cfg.publicSlug.trim();
}
}
} catch (e) {
console.warn(`Failed to read publicSlug for ${experimentName}:`, e.message);
}
if (slugToExperimentName[publicSlug] && slugToExperimentName[publicSlug] !== experimentName) {
console.warn(`Slug collision detected for '${publicSlug}'. Using first mapping to '${slugToExperimentName[publicSlug]}'.`);
} else {
slugToExperimentName[publicSlug] = experimentName;
}
experimentNameToSlug[experimentName] = publicSlug;
});
// Middleware to validate experiment existence
function checkExperimentValidity(req, res, next) {
const { experimentName } = req.params;
if (!validExperiments.includes(experimentName)) {
return res.status(404).send('Not An Existing Path');
}
next();
}
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));
// session middleware setup
app.use(session({
secret: 'pyJmic-hoqjeq-4kodto',
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: 1000 * 60 * 60 * 24 // 24 hours
} // TODO: set secure to true in production
}));
// Middleware to assign a `user_id` if not present
app.use((req, res, next) => {
if (!req.session.user_id) {
const prolificId = req.query.PROLIFIC_PID; // Check for PROLIFIC_PID in query params
const sonaId = req.query.SONA_ID; // Check for SONA_ID in query params
if (prolificId) {
// 'PROLIFIC-{PROLIFIC_PID}' is used as the user_id for Prolific studies
req.session.user_id = `PROLIFIC-${prolificId}`;
console.log(`Prolific ID assigned: ${req.session.user_id}`);
} else if (sonaId) {
// 'SONA-{SONA_ID}' is used as the user_id for SONA studies
req.session.user_id = `SONA-${sonaId}`;
console.log(`SONA ID assigned: ${req.session.user_id}`);
} else {
req.session.user_id = uuidv4();
console.log(`UUID assigned: ${req.session.user_id}`);
}
} else {
// check if PROLIFIC_PID or SONA_ID is present in query params, if so, update the user_id
const prolificId = req.query.PROLIFIC_PID;
const sonaId = req.query.SONA_ID;
if (prolificId) {
req.session.user_id = `PROLIFIC-${prolificId}`;
console.log(`Prolific ID updated: ${req.session.user_id}`);
} else if (sonaId) {
req.session.user_id = `SONA-${sonaId}`;
console.log(`SONA ID updated: ${req.session.user_id}`);
}
}
next();
});
app.post('/verify-captcha', async (req, res) => {
const token = req.body.token || req.query.token;
if (!token) {
return res.status(400).json({ success: false, error: 'No token provided' });
}
try {
const response = await axios.post(
'https://www.google.com/recaptcha/api/siteverify',
null, // no body
{
params: {
secret: RECAPTCHA_SECRET_KEY,
response: token
}
}
);
if (response.data.success) {
res.json({ success: true });
} else {
res.json({ success: false, error: 'reCAPTCHA failed' });
}
} catch (err) {
console.error(err);
res.status(500).json({ success: false, error: 'Verification error' });
}
});
// middleware to parse JSON bodies
app.use(bodyParser.json());
// serve static files from dist directory (Webpack's output)
app.use(express.static('dist'));
app.use('/FIT5item_stimuli', express.static(path.join(__dirname, 'Experiments/FIT5item_Exp/stimuli')));
// middleware to check experiment completion
function checkExperimentCompletion(experimentName) {
return function(req, res, next) {
if (req.session[experimentName]) {
next(); // Proceed to the next middleware or route handler
} else {
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Redirecting</title>
<script>
setTimeout(function() {
window.location.href = '/start-experiment/${experimentNameToSlug[experimentName] || experimentName}';
}, 5000);
</script>
</head>
<body>
<h1>Please complete the ${experimentName} experiment first.</h1>
<p>You will be redirected to the homepage in 5 seconds...</p>
</body>
</html>
`);
}
};
}
// server input.txt for easier use VOQ
app.get('/input-file', (req, res) => {
const filePath = path.join(__dirname, 'Experiments', 'VOQ_Exp', 'custom_js', 'input.txt');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading input.txt:', err);
return res.status(500).send('Error reading the file.');
}
res.status(200).send(data);
});
});
// Route to complete experiment
app.post('/complete-experiment/:experimentName', async (req, res) => {
const experimentName = req.params.experimentName;
const dataDir = path.join(__dirname, 'Data', `${experimentName}_Data`);
const assignmentTablePath = path.join(dataDir, 'assignment_table.csv');
// Ensure Data directory exists
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
if (fs.existsSync(assignmentTablePath)) {
// Acquire a lock on the assignment table
const release = await lockfile.lock(assignmentTablePath, { retries: 5 });
// Read and update the assignment table
const assignmentData = fs.readFileSync(assignmentTablePath, 'utf8')
.split('\n')
.map(row => row.split(','))
.filter(row => row.length > 1);
const userId = getUserId(req);
if (!userId) {
return res.status(400).send('User ID not found in request or session');
}
const userEntryIndex = assignmentData.findIndex(row => row[0] === userId);
if (userEntryIndex !== -1) {
// Update the finished status to 1 for the participant
assignmentData[userEntryIndex][2] = '1';
fs.writeFileSync(assignmentTablePath, assignmentData.map(row => row.join(',')).join('\n'), 'utf8');
} else {
console.error('User ID not found in assignment table:', userId);
}
// Release the lock
await release();
}
// Redirect to the end-experiment route, preserving query params
req.session[experimentName] = true;
let redirectURL = `/end-experiment/${experimentName}`;
const sonaId = req.body?.SONA_ID || req.query.SONA_ID;
if (sonaId) {
redirectURL += `?SONA_ID=${encodeURIComponent(sonaId)}`;
}
res.redirect(redirectURL);
});
// Function to write headers if the file is empty
function writeHeadersIfEmpty(filePath, headers) {
if (!fs.existsSync(filePath) || fs.statSync(filePath).size === 0) {
fs.writeFileSync(filePath, headers + '\n', 'utf8');
}
}
// Helper function to get user_id from request (body, query, or session)
function getUserId(req) {
// First, check request body for user_id
if (req.body && req.body.user_id) {
return req.body.user_id;
}
// Second, check for PROLIFIC_PID in body or query params
const prolificId = req.body?.PROLIFIC_PID || req.query.PROLIFIC_PID;
if (prolificId) {
return `PROLIFIC-${prolificId}`;
}
// Third, check for SONA_ID in body or query params
const sonaId = req.body?.SONA_ID || req.query.SONA_ID;
if (sonaId) {
return `SONA-${sonaId}`;
}
// Finally, fall back to session
return req.session.user_id;
}
function parseAudioDataUrl(dataUrl) {
if (typeof dataUrl !== 'string') return null;
const extByMime = {
'audio/webm': 'webm',
'audio/wav': 'wav',
'audio/x-wav': 'wav',
'audio/ogg': 'ogg',
'audio/mp4': 'm4a',
'audio/mpeg': 'mp3'
};
// Case 1: full data URL → data:audio/webm;base64,GkXf...
const m = dataUrl.match(/^data:(audio\/[a-zA-Z0-9.+-]+);base64,(.+)$/s);
if (m) {
const mime = m[1];
return {
mime,
ext: extByMime[mime] || 'webm',
buffer: Buffer.from(m[2], 'base64')
};
}
// Case 2: raw base64 (no data-URL prefix) – jsPsych html-audio-response
// sends the blob directly as base64 without the "data:audio/...;base64," header.
// Validate it is plausibly base64 (only base64 chars + padding).
if (/^[A-Za-z0-9+/]+=*$/.test(dataUrl.trim())) {
return {
mime: 'audio/webm',
ext: 'webm',
buffer: Buffer.from(dataUrl.trim(), 'base64')
};
}
return null;
}
app.post('/save-data/:experimentName', (req, res) => {
console.log('Received data for saving:', req.body.data);
const experimentName = req.params.experimentName;
const userId = getUserId(req);
if (!userId) {
return res.status(400).send('User ID not found in request or session');
}
const csvFilePath = `Data/${experimentName}_Data/${userId}.csv`;
// --- AUDIO ADD-ON: save any audio-response trials to disk ---
// Lazily create audio dir only when we actually save audio.
let audioDir = null;
req.body.data.forEach((trial, i) => {
if (!trial || trial.trial_type !== 'html-audio-response') return;
const parsed = parseAudioDataUrl(trial.response);
if (!parsed) return;
const rawStimulus = typeof trial.stimulus === 'string' && trial.stimulus.trim()
&& !trial.stimulus.trim().startsWith('<')
? trial.stimulus.trim().replace(/\.[a-zA-Z0-9]+$/, '') // strip e.g. .jpg
: null;
const stimulusLabel = rawStimulus
? rawStimulus.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64) // sanitize + length cap
: String(Number.isFinite(trial.trial_index) ? trial.trial_index : i);
const modality = rawStimulus === null
? 'unknown'
: /\.(jpg|jpeg|png|gif|bmp|webp|svg)$/i.test(trial.stimulus?.trim() || '')
? 'image'
: 'word';
if (!audioDir) {
audioDir = path.join(__dirname, 'Data', `${experimentName}_Data`, 'audio');
fs.mkdirSync(audioDir, { recursive: true });
}
const fileName = `${userId}_${modality}_${stimulusLabel}.${parsed.ext}`;
const absPath = path.join(audioDir, fileName);
try {
fs.writeFileSync(absPath, parsed.buffer);
// Replace the huge base64 blob with a filepath reference in the CSV
trial.audio_file = path.join('audio', fileName);
trial.audio_mime = parsed.mime;
trial.audio_modality = modality;
trial.response = '[audio_saved]';
} catch (err) {
console.error(`Error saving audio for stimulus ${stimulusLabel}:`, err);
}
});
// --- END AUDIO ADD-ON ---
// Predefined fields
const predefinedFields = [
'rt',
'question',
'response',
'time_elapsed',
'trial_index',
'accuracy',
'audio_file', // new
'audio_mime', // new
'audio_modality' // new
];
// Load experiment-specific included columns from config file
let includedColumns = null; // null means include everything (default)
try {
const configPath = `Experiments/${experimentName}_Exp/config.json`;
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
includedColumns = config.includedColumns || null;
}
} catch (error) {
console.log(`No config file found for experiment ${experimentName}, using default include everything`);
}
// Dynamically generate the header based on all keys in `req.body.data`
const dynamicFieldsSet = new Set();
function getAllKeys(obj) {
return Object.keys(obj);
}
req.body.data.forEach(item => {
getAllKeys(item).forEach(key => {
// Check if the key is not in predefinedFields
if (!predefinedFields.includes(key)) {
// If includedColumns is specified, only include those columns
// If includedColumns is null (default), include everything
if (includedColumns === null || includedColumns.includes(key)) {
dynamicFieldsSet.add(key);
}
}
});
});
// console.log(req.body.data);
// Convert the Set to an Array
const dynamicFields = Array.from(dynamicFieldsSet);
// Combine predefined fields with dynamic fields
let headers = [...predefinedFields, ...dynamicFields];
if (experimentName.toUpperCase() === 'VOQ') {
// insert subjectID & sceneID and put question_num before question
headers = ['subjectID', 'sceneID', ...headers];
const qIdx = headers.indexOf('question');
if (qIdx !== -1 && !headers.includes('question_num')) {
headers.splice(qIdx, 0, 'question_num');
}
}
fs.unlinkSync(csvFilePath);
// Write headers if the file is empty
writeHeadersIfEmpty(csvFilePath, headers.join(','));
const csvWriter = createCsvWriter({
path: csvFilePath,
header: headers.map(field => ({
id: field,
title: field.replace(/_/g, ' ').toUpperCase()
})),
append: true
});
const flattenedData = [];
req.body.data.forEach(item => {
if (item.response && typeof item.response === 'object') {
Object.keys(item.response).forEach(key => {
const newItem = {
...item, // Copy all fields from the original item
response: item.response[key], // Set the response to the value of the key
question: key // Add the key as the question field
};
delete newItem.response[key]; // Remove the original nested response
flattenedData.push(newItem); // Add the new item to the flattened data array
});
} else {
// If response is not a JSON object, keep the item as it is
flattenedData.push(item);
}
});
let currentScene = 'NA';
let voqQCounter = 0;
// TODO: might need a bit cleanup
const formattedData = flattenedData.map(trial => {
const isVOQ = experimentName.toUpperCase() === 'VOQ';
if (trial.id && /^scene_\d+$/i.test(String(trial.id))) {
currentScene = String(trial.id);
}
let question_num = 'NA';
let question_text = (typeof trial.question === 'string' && trial.question.trim()) ? trial.question.trim() : 'NA';
if (isVOQ) {
const isSceneRow = (trial.id && /^scene_\d+$/i.test(String(trial.id)));
const isQuestionType = (trial.expType === 'multchoice_other' || trial.expType === 'likert');
// if likert and question is Q0/Q1/Q2, map to actual wording
if (trial.expType === 'likert' && typeof trial.question === 'string' && /^Q\d+$/i.test(trial.question)) {
let texts = trial.question_texts;
if (typeof texts === 'string') {
texts = texts.split(',').map(s => s.trim());
}
if (Array.isArray(texts)) {
const idx = parseInt(trial.question.slice(1), 10);
if (Number.isFinite(idx) && texts[idx]) {
question_text = texts[idx];
}
}
if (question_text === 'NA' && typeof trial.prompt === 'string' && trial.prompt.trim()) {
question_text = trial.prompt.trim();
}
} else {
if (question_text === 'NA' && typeof trial.prompt === 'string' && trial.prompt.trim()) {
question_text = trial.prompt.trim();
}
}
if (currentScene !== 'NA' && !isSceneRow && isQuestionType) {
voqQCounter += 1;
question_num = `Q${voqQCounter}`;
}
}
// Base data with predefined fields
const baseData = {
...(isVOQ ? { subjectID: userId, sceneID: currentScene, question_num, question: question_text } : {
question: question_text
}),
rt: trial.rt || 'NA',
response: typeof trial.response !== 'undefined' && trial.response !== null
? trial.response : 'NA',
time_elapsed: trial.time_elapsed || 'NA',
trial_index: trial.trial_index || 'NA',
accuracy: trial.grade && trial.grade == 1 ? (trial.response == trial.ans ? 1 : 0) : 'NA',
list: trial.list || 'NA',
stimulus: trial.stimulus || 'NA'
};
// Add all other fields dynamically, respecting includedColumns filter
dynamicFields.forEach(field => {
baseData[field] = trial[field] || null; // Include only allowed fields
});
return baseData;
});
csvWriter.writeRecords(formattedData)
.then(() => {
res.status(200).send('Data saved successfully');
})
.catch(error => {
console.error('Error writing to CSV file:', error);
res.status(500).send('Internal Server Error');
});
});
app.post('/feedback/:experimentName', async (req, res) => {
const experimentName = req.params.experimentName;
const userId = getUserId(req);
if (!userId) {
return res.status(400).json({ error: 'User ID not found in request or session' });
}
const feedbackDir = path.join(__dirname, 'Data', `${experimentName}_Data`);
const feedbackCsvFilePath = path.join(feedbackDir, 'feedback.csv');
const headers = 'User ID,Feedback';
const contactCsvFilePath = path.join(feedbackDir, 'participant_contact.csv');
const legacyContactCsvFilePath = path.join(feedbackDir, 'SONA_contact.csv');
const contactHeaders = 'User ID,Name,Email,FutureContact';
// Ensure the directory exists
if (!fs.existsSync(feedbackDir)) {
fs.mkdirSync(feedbackDir, { recursive: true });
}
// Backward compatibility: migrate legacy filename to neutral filename.
if (fs.existsSync(legacyContactCsvFilePath) && !fs.existsSync(contactCsvFilePath)) {
try {
fs.renameSync(legacyContactCsvFilePath, contactCsvFilePath);
} catch (err) {
console.error('Error migrating contact file name:', err);
return res.status(500).json({ error: 'Error migrating contact file name' });
}
}
// Ensure the file exists safely
if (!fs.existsSync(feedbackCsvFilePath)) {
try {
fs.writeFileSync(feedbackCsvFilePath, `${headers}\n`, { flag: 'wx' }); // Atomically create the file with headers
} catch (err) {
if (err.code !== 'EEXIST') { // Ignore error if another process created the file
console.error('Error creating feedback file:', err);
res.status(500).json({ error: 'Error initializing feedback file' });
return;
}
}
}
try {
/////// SAVE CONTACT START ///////
if (req.body.future_contact === 'Yes' && req.body.participantName && req.body.participantEmail) {
if (!fs.existsSync(contactCsvFilePath)) {
try {
fs.writeFileSync(contactCsvFilePath, `${contactHeaders}\n`, { flag: 'wx' });
} catch (err) {
if (err.code !== 'EEXIST') {
console.error('Error creating contact file:', err);
return res.status(500).json({ error: 'Error initializing contact file' });
}
}
}
const releaseContact = await lockfile.lock(contactCsvFilePath, { retries: 5 });
const contactWriter = createCsvWriter({
path: contactCsvFilePath,
header: [
{ id: 'user_id', title: 'User ID' },
{ id: 'name', title: 'Name' },
{ id: 'email', title: 'Email' },
{ id: 'future_contact', title: 'FutureContact' }
],
append: true
});
await contactWriter.writeRecords([{
user_id: userId,
name: req.body.participantName,
email: req.body.participantEmail,
future_contact: req.body.future_contact
}]);
await releaseContact();
}
/////// SAVE CONTACT END ///////
// Acquire a lock on the feedback.csv file
const release = await lockfile.lock(feedbackCsvFilePath, { retries: 5 });
// Write feedback data
const csvWriter = createCsvWriter({
path: feedbackCsvFilePath,
header: [
{ id: 'user_id', title: 'User ID' },
{ id: 'feedback', title: 'Feedback' }
],
append: true // Append data to the file
});
const feedbackData = {
user_id: userId,
feedback: req.body.feedback // Request body parsed feedback
};
await csvWriter.writeRecords([feedbackData]);
// Release the lock
await release();
// Retrieve confirmation URL from the experiment's configuration file
configFilePath = path.join(__dirname, 'Experiments', `${experimentName}_Exp`, 'config.json');
if (fs.existsSync(configFilePath)) {
const config = JSON.parse(fs.readFileSync(configFilePath, 'utf8'));
if (config.confirmationURL) {
let redirectURL = config.confirmationURL;
const sonaId = req.body?.SONA_ID || req.query.SONA_ID;
if (sonaId) {
redirectURL += `&survey_code=${encodeURIComponent(sonaId)}`;
}
res.status(200).json({ message: 'Feedback submitted successfully', prolificCompletionUrl: redirectURL });
} else {
res.status(200).json({ message: 'Feedback submitted successfully' });
}
} else {
res.status(200).json({ message: 'Feedback submitted successfully' });
}
} catch (err) {
console.error('Error writing to CSV file or acquiring lock:', err);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/save-audio/:experimentName/:audioId', bodyParser.raw({ type: 'audio/webm', limit: '10mb' }), (req, res) => {
const user_id = getUserId(req);
if (!user_id) {
return res.status(400).send('User ID not found in request or session');
}
const audio_id = req.params.audioId;
console.log('Received audio data:', audio_id);
const audioFilePath = path.join(__dirname, 'Data', `${req.params.experimentName}_Data`, `${user_id}_${audio_id}.webm`);
// Write the raw audio data to a file
fs.writeFile(audioFilePath, req.body, (err) => {
if (err) {
console.error('Error saving audio file:', err);
return res.status(500).send('Failed to save audio file');
}
console.log('Audio file saved at:', audioFilePath);
res.status(200).send('Audio file uploaded successfully');
});
});
// endpoint to reset session
app.post('/reset-session/:experimentName', (req, res) => {
const experimentName = req.params.experimentName;
if (req.session) {
req.session[experimentName] = false; // Reset the specific experiment completion status
req.session[`${experimentName}Feedback`] = false; // Reset feedback submission status
res.status(200).json({ success: true });
} else {
res.status(500).json({ success: false, message: 'Failed to reset session' });
}
});
// API to get session state
app.get('/session-state/:experimentName', (req, res) => {
const experimentName = req.params.experimentName;
res.json({
feedbackSubmitted: req.session[`${experimentName}Feedback`] || false
});
});
// Route for starting an experiment (supports public slug or internal name)
app.get('/start-experiment/:slugOrName', async (req, res) => {
const { slugOrName } = req.params;
const experimentName = slugToExperimentName[slugOrName] || (validExperiments.includes(slugOrName) ? slugOrName : null);
if (!experimentName) {
return res.status(404).send('Not An Existing Path');
}
const filePath = path.join(__dirname, 'dist', `${experimentName}.html`);
// 1) Make sure the requested file actually exists.
if (!fs.existsSync(filePath)) {
return res.status(404).send('Not An Existing Path');
}
// 2) Ensure data directory, read config, set up assignment, etc.
const dataDir = path.join(__dirname, 'Data', `${experimentName}_Data`);
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
const configFilePath = path.join(__dirname, 'Experiments', getExperimentFolder(experimentName), 'config.json');
if (fs.existsSync(configFilePath)) {
const config = JSON.parse(fs.readFileSync(configFilePath, 'utf8'));
if (config.indexes) {
const indexes = config.indexes;
console.log('Indexes:', indexes);
const counterPath = path.join(dataDir, 'counter.csv');
const assignmentTablePath = path.join(dataDir, 'assignment_table.csv');
// Create counter.csv & assignment_table.csv if needed
[counterPath, assignmentTablePath].forEach(filePath => {
if (!fs.existsSync(filePath)) {
try {
const header =
filePath === counterPath
? 'idx,total_assigned\n'
: 'ID,assigned,finished\n';
fs.writeFileSync(filePath, header, { flag: 'wx' });
} catch (err) {
if (err.code !== 'EEXIST') {
console.error(`Error creating file (${filePath}):`, err);
return res.status(500).json({ error: 'Error initializing files' });
}
}
}
});
// 3) Lock/update assignment and set req.session.idx
try {
const counterRelease = await lockfile.lock(counterPath, { retries: 5 });
const assignmentRelease = await lockfile.lock(assignmentTablePath, { retries: 5 });
const assignmentData = fs
.readFileSync(assignmentTablePath, 'utf8')
.split('\n')
.map(row => row.split(','))
.filter(row => row.length > 1);
const userId = getUserId(req);
if (!userId) {
return res.status(400).send('User ID not found in request or session');
}
let assignedIdx = null;
const userEntry = assignmentData.find(row => row[0] === userId);
if (userEntry) {
// If they've already been assigned
if (userEntry[2] === '1') {
// finished
req.session.idx = 'done';
} else {
// same assignment
assignedIdx = parseInt(userEntry[1], 10);
req.session.idx = assignedIdx;
}
} else {
// new user => assign an index from config.indexes
assignedIdx = indexes[(assignmentData.length - 1) % indexes.length];
assignmentData.push([userId, assignedIdx.toString(), '0']);
fs.writeFileSync(
assignmentTablePath,
assignmentData.map(row => row.join(',')).join('\n'),
'utf8'
);
req.session.idx = assignedIdx;
}
// update the counter file if this is new assignment
if (!userEntry && assignedIdx !== null) {
const counterData = fs
.readFileSync(counterPath, 'utf8')
.split('\n')
.map(row => row.split(','))
.filter(row => row.length > 1);
const idxEntry = counterData.find(row => row[0] === assignedIdx.toString());
if (idxEntry) {
idxEntry[1] = (parseInt(idxEntry[1]) + 1).toString();
} else {
counterData.push([assignedIdx.toString(), '1']);
}
fs.writeFileSync(
counterPath,
counterData.map(row => row.join(',')).join('\n'),
'utf8'
);
}
// release locks
await counterRelease();
await assignmentRelease();
// ensure user has a CSV file started
const csvFilePath = path.join(dataDir, `${userId}.csv`);
if (!fs.existsSync(csvFilePath)) {
fs.closeSync(fs.openSync(csvFilePath, 'w'));
}
} catch (err) {
console.error('Error in assignment logic:', err);
return res.status(500).json({ error: 'Internal server error' });
}
}
}
// 4) Finally, send the experiment page **after** session.idx has been set
return res.sendFile(filePath);
});
app.post('/get-idx', (req, res) => {
if (req.session.idx) {
res.json({ idx: req.session.idx });
} else {
res.json({ idx: null });
}
});
app.get('/end-experiment/:experimentName', checkExperimentValidity, (req, res, next) => {
const { experimentName } = req.params;
const filePath = path.join(__dirname, 'dist', `${experimentName}end.html`);
if (fs.existsSync(filePath)) {
// Apply completion check middleware
checkExperimentCompletion(experimentName)(req, res, () => {
res.sendFile(filePath);
});
} else {
res.status(404).send('Not An Existing Path');
}
});
/*
DO NOT UPDATE ROUTES FOR NEW EXPERIMENTS AFTER THIS LINE
YOU WILL GET AN ERROR: "Not An Existing Path"
*/
// Catch-all route to return 404 for all other routes
app.get('*', (req, res) => {
res.status(404).send('Not An Existing Path');
});
// Specific route to handle root path
app.get('/', (req, res) => {
res.status(404).send('Not An Existing Path');
});
app.listen(port, '0.0.0.0', () => {
console.log(`Server is running on http://localhost:${port}`);
});