-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
153 lines (134 loc) · 3.7 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
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
// set up ======================================================================
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var port = 3000;
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost:27017/db';
// configuration ===============================================================
// Use connect method to connect to the Server
mongoose.connect(url);
app.use(express.static('./public'));
//app.use(bodyParser.json()); // parse application/json
//app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', 'extended': 'true'}));
app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request
// routes ======================================================================
var Participant = mongoose.model('Participant', {
name: Number,
appearance: {
age: String,
gender: String,
glasses: String,
ethnicity: String
},
images: [
{
imageName: String,
trueValence: Number,
trueArousal: Number,
samValence: Number,
samArousal: Number,
inputs: [
{
timestamp: Number,
emotions: {
joy: Number,
anger: Number,
contempt: Number,
disgust: Number,
engagement: Number,
fear: Number,
sadness: Number,
surprise: Number,
valence: Number,
}
}
]
}
]
});
var Images = mongoose.model('Images', {
participantId: Number,
imageName: String,
trueValence: Number,
trueArousal: Number,
samValence: Number,
samArousal: Number,
inputs: [
{
timestamp: Number,
emotions: {
joy: Number,
anger: Number,
contempt: Number,
disgust: Number,
engagement: Number,
fear: Number,
sadness: Number,
surprise: Number,
valence: Number,
}
}
]
});
function getAll(res) {
Participant.find(function (err, all) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err) {
res.send(err);
}
res.json(all); // return all todos in JSON format
});
};
app.post('/api/newUser', function (req, res) {
var previosParticipantId = 0;
Participant.findOne().sort('-name').select('name').exec(function (err, participant) {
if (err)
res.send(err);
console.log("participant: " + participant);
if (!participant) {
} else {
previosParticipantId = participant.name;
console.log("id: " + previosParticipantId);
}
console.log("prev id: " + previosParticipantId);
var newId = previosParticipantId+1;
console.log("new id: " + newId);
Participant.create({
name: newId,
appearance: req.body.appearance,
images: []
}, function (innerErr, participant) {
if (innerErr)
res.send(innerErr);
res.json(participant);
});
});
});
// POST method route
app.post('/api/saveNewImage', function (req, res) {
Images.create({
participantId: req.body.participantId,
imageName: req.body.imageName,
trueValence: req.body.trueValence,
trueArousal: req.body.trueArousal,
samValence: req.body.samValence,
samArousal: req.body.samArousal,
inputs: req.body.inputs
}, function(err, doc) {
if (err)
res.send(err);
res.sendStatus(200);
//getAll(res);
});
})
app.get('*', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);