-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (47 loc) · 1.69 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
var mongodb = require('mongodb');
var ObjectId = require('mongodb').ObjectID;
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/decisions';
var express = require('express')
var app = express();
var router = express.Router();
var bodyParser = require('body-parser');
var addDecision_controller = require('./controllers/addDecision_controller');
var getNewest_controller = require('./controllers/getNewestDecision_controller');
var voteChoiceA_controller = require('./controllers/voteChoiceA_controller');
var voteChoiceB_controller = require('./controllers/voteChoiceB_controller');
app.use(express.static(__dirname ));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.get('/', function (req, res, next) {
try {
res.sendFile('./index.html', {root: __dirname});
} catch (e) {
next(e)
}
})
app.get('/api/getNewest', function(req, res) {
try {
MongoClient.connect(url, function(err, db) {
db.collection('decisions').find().sort({'decision_id': -1}).toArray(function(err, docs) {
if (err) throw (err);
if (docs.length != 0) {
console.log(docs[0])
res.json(docs[0]);
} else {
res.send(null);
}
});
});
} catch (e) {
throw(e);
}
});
app.listen(process.env.PORT || 3000, function () {
console.log('Listening on http://localhost:' + (process.env.PORT || 3000))
})
app.post('/api/addDecision', addDecision_controller.view);
app.post('/api/voteChoiceA', voteChoiceA_controller.view);
app.post('/api/voteChoiceB', voteChoiceB_controller.view);