-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
111 lines (92 loc) · 3 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
var express = require('express');
var bodyParser = require('body-parser');
var mongodb = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var app = express();
var port = 1927;
var db, collection;
var uri = "mongodb://" + process.env.MONGOLAB_USER + ":" + process.env.MONGOLAB_PASSWORD + "@ds023485.mlab.com:23485/webdev";
var jokes=[{setup:"Our wedding was so beautiful,",punchline:"even the cake was in tiers", votes: 0},{setup:"I'm reading a book on the history of glue",punchline:"I just can't seem to put it down", votes: 0},{setup:"What do you call an Argentinian with a rubber toe?",punchline:"Roberto", votes: 0}];
app.use(bodyParser.json());
app.use(express.static('static'));
mongodb.connect(uri, function(err, database) {
db = database;
collection = db.collection('jokes');
app.listen(process.env.PORT || port, function() {
console.log("Listening on " + port);
});
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/static/index.html');
});
app.get('/jokes', function(req, res) {
collection.count(function(err, count) {
var randomNumber = Math.floor(Math.random() * count);
collection.find().limit(-1).skip(randomNumber).next(
function(err, results) {
res.send(results);
}
);
});
});
app.put('/upvote', function(req, res) {
var jokeIndex = req.body.id;
collection.findOneAndUpdate(
{ _id: ObjectId(jokeIndex) },
{ $inc: { "votes" : 1 }},
function(err, result) {
result.value.votes++;
res.send(result.value);
}
);
});
app.put('/downvote', function(req, res) {
var jokeIndex = req.body.id;
collection.findOneAndUpdate(
{ _id: ObjectId(jokeIndex) },
{ $inc: { "votes" : -1 }},
{ returnNewDocument: true },
function(err, result) {
result.value.votes--;
res.send(result.value);
}
);
});
/* This page was to populate some jokes into the database */
// app.get('/admin', function(req, res) {
//
// console.log('Attempting to add into database.');
// mongodb.connect(uri, function(err, db) {
// var collection = db.collection('jokes');
// collection.insertMany(jokes,
// function(err, results){
// res.send(results);
// db.close();
// }
// );
// });
// });
app.get('/updatejokes', function(req, res) {
res.sendFile(__dirname + '/static/updateJokes.html');
});
app.post('/createjoke', function(req, res) {
collection.insertOne(req.body,
function(err, results){
res.send(results);
}
);
});
app.get('/alljokes', function(req, res) {
collection.find().toArray(
function(err, resultArray) {
res.send(resultArray)
}
);
});
app.delete('/deletejoke', function(req, res) {
collection.deleteOne({_id: ObjectId(req.body.id)},
function(err, results){
res.send(results);
}
);
});