-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
70 lines (56 loc) · 1.93 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
// --- LOADING MODULES
var express = require('express'),
mongoose = require('mongoose'),
bodyParser = require('body-parser');
// --- INSTANTIATE THE APP
var app = express();
// --- MONGOOSE SETUP
const URI = "mongodb+srv://yash_verma:*********@jspsych-eymdu.mongodb.net/test?retryWrites=true&w=majority"
const connectDB = async () => {
try{
await mongoose.connect(URI,{
useNewUrlParser: true ,
useUnifiedTopology: true });
console.log('Mongodb is connected');
}catch (e) {
console.error(e);
console.log('oops!!');
}
};
connectDB().catch(console.error);
var emptySchema = new mongoose.Schema({}, { strict: false });
var Entry = mongoose.model("Entry", emptySchema);
// --- STATIC MIDDLEWARE
app.use('/jspsych-6.0.5', express.static(__dirname + "/jspsych-6.0.5"));
app.use('/css', express.static(__dirname + "/css"));
app.use('/img', express.static(__dirname + "/img"));
// --- BODY PARSING MIDDLEWARE
app.use(bodyParser.json()); // to support JSON-encoded bodies
// --- VIEW LOCATION, SET UP SERVING STATIC HTML
app.set('views', __dirname + '/');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// --- ROUTING
app.get('/', function(request, response) {
response.render('index.html');
});
app.get('/experiment', function(request, response) {
response.render('test.html');
});
app.post("/experiment-data", (req, res) => {
var myData = new Entry(req.body);
myData.save()
.then(item => {
res.send("saved to database");
})
.catch(err => {
res.status(400).send("unable to save to database");
});
});
app.get('/finish', function(request, response) {
response.render('finish.html');
});
// --- START THE SERVER
var server = app.listen(process.env.PORT || 7777, function(){
console.log("Listening on port %d", server.address().port);
});