-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (110 loc) · 2.95 KB
/
Copy pathindex.js
File metadata and controls
118 lines (110 loc) · 2.95 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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var mongodb = require('mongodb');
var mongo = mongodb.MongoClient;
var mongoip = 'mongodb://localhost:27017';
var port = process.env.PORT || 3000;
var id = process.env.IP || '0.0.0.0';
var data;
app.set('views', __dirname + '/src/');
app.set('view engine', 'jade');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/src/'));
//favicon
app.get('/create', function(req, res) {
res.render('create');
});
app.post('/$', function(req, res, next) {
if (req.body.option) {
let col = data.collection('survey');
let doc = { title: req.body.title };
let options = req.body.option;
let opts = [];
for (let a = req.body.option.length - 1; a >= 0; a--)
opts[a] = { qid: a, option: options[a], score: 0 };
doc.options = opts;
doc.total = 0;
console.log(doc);
col.insert(doc, function(err){
if (!err)
req.msg = 2;
else {
req.msg = 1;
}
next();
});
} else next();
});
app.post('/$', function(req, res, next) {
if (req.body.choice) {
let parsed = parseInt(req.body.choice);
console.log(parsed);
if (!isNaN(parsed)) {
let get = 'options.' + parsed + '.score';
let col = data.collection('survey');
console.log(req.body.id + ' ' + get);
col.update({ '_id': new mongodb.ObjectID(req.body.id), 'options.qid': parsed },
{ '$inc': { 'options.$.score': 1, 'total': 1 } },
function(err, result) {
if (!err) {
req.msg = 3;
prev = req.body.id;
} else {
console.log(err);
req.msg = 1;
}
next();
}
);
}
} else next();
});
app.use('/$', function(req, res, next){
let col = data.collection('survey');
let q = new mongodb.ObjectID(req.body.id);
let doc = col.find({ '_id': q }).next(function(err, doc){
if (doc) {
let retthis = { document: doc };
let total = 0;
for (let a = 0; a < retthis.document.options.length; a++) {
retthis.document.options[a].size = Math.round(10000*(retthis.document.options[a].score/retthis.document.total))/100;
}
res.prevDoc = doc;
}
next();
});
}, (req, res) => {
let col = data.collection('survey');
let find = col.find();
find.count(function(err, count) {
let random = Math.floor(Math.random()*count);
console.log(count + ' ' + random);
col.find().limit(-1).skip(random).next(function(err, doc){
if (err) {
res.send('error');
} else {
let returnthis = { document: doc, msg: req.msg };
for (let a = 0; a < doc.options.length; a++) {
console.log(doc.options[a]);
}
returnthis.prev = res.prevDoc;
if (res.prevDoc)
returnthis.prevExists = "none";
else
returnthis.prevExists = "default";
console.log(returnthis);
res.render('index', returnthis);
}
});
});
});
mongo.connect(mongoip, function (err, db) {
if (db) {
data = db;
app.listen(port, id, function(){
console.log('Connected on ' + id + ":" + port);
});
}
});