-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (44 loc) · 1.53 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
// --- LOADING MODULES
var express = require('express'),
body_parser = require('body-parser'),
mongoose = require('mongoose');
// --- INSTANTIATE THE APP
var app = express();
// --- MONGOOSE SETUP
mongoose.connect(process.env.CONNECTION || 'mongodb://localhost/jspsych');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function callback() {
console.log('database opened');
});
var emptySchema = new mongoose.Schema({}, { strict: false });
var Entry = mongoose.model('Entry', emptySchema);
// --- STATIC MIDDLEWARE
app.use(express.static(__dirname + '/public'));
app.use('/jsPsych', express.static(__dirname + "/jsPsych"));
// --- BODY PARSING MIDDLEWARE
app.use(body_parser.json()); // to support JSON-encoded bodies
// --- VIEW LOCATION, 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('experiment.html');
});
app.get('/experiment', function(request, response) {
response.render('experiment.html');
});
app.post('/experiment-data', function(request, response){
Entry.create({
"data":request.body
});
response.end();
})
app.get('/finish', function(request, response){
response.render('finish.html');
})
// --- START THE SERVER
var server = app.listen(process.env.PORT || 3000, function(){
console.log("Listening on port %d", server.address().port);
});