-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
108 lines (92 loc) · 3.64 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
// set consts for express environment, mongoclient, .env and PORT
const express = require('express'),
app = express(),
MongoClient = require('mongodb').MongoClient,
PORT = process.env.PORT || 8000,
ObjectId = require('mongodb').ObjectId
require('dotenv').config()
//connect to DB
let db,
dbConnectionStr = process.env.DB_STRING,
dbName = 'schidtter'
MongoClient.connect(dbConnectionStr, { useUnifiedTopology: true })
.then(client => {
console.log(`Connected to ${dbName} database`)
db = client.db(dbName)
})
//set view engine to EJS, static public folder for CSS, IMG, and JS routing, init JSON method
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
//render ejs on load
app.get('/', (request, response) => {
db.collection('schidtter').find().sort({upvote: -1}).toArray()
.then(data => {
response.render('index.ejs', { info: data })
})
.catch(err => console.error(err))
})
//add a new post to the main page
app.post('/addPost', (request, response) => {
db.collection('schidtter').insertOne({
userName: request.body.username,
userPost: request.body.postForText,
upvote: 1,
downvote: 0
})
.then(result => {
console.log('new post created')
response.redirect('/')
})
.catch(err => console.log(err))
})
//adds one like to each post ---
app.put(`/addOneUpvote`, async (request, response) => {
console.log(`getting there ${request.body.info}`)
console.log(request.body.upvote)
const query = { "_id": ObjectId(`${request.body.info}`) }
const update = {
$inc: {
upvote: 1
}
}
db.collection('schidtter').updateOne(query, update)
.then(result => {
console.log(`Got some upvote action ${request.body.info}`)
response.json('Upvote be done, yo')
})
.catch(err => console.log(err))
})
//need to add a downvote here
app.put(`/addOneDownvote`, async (request, response) => {
console.log(`getting there ${request.body.info}`)
console.log(request.body.upvote)
const query = { "_id": ObjectId(`${request.body.info}`) }
const update = {
$inc: {
downvote: 1
}
}
db.collection('schidtter').updateOne(query, update)
.then(result => {
console.log(`Got some downvote action ${request.body.info}`)
response.json('Upvote be done, yo')
})
.catch(err => console.log(err))
})
//delete to delete the post
app.delete(`/deleteOnePost`, async (request, response) => {
console.log(`getting there ${request.body.info}`)
const query = { "_id": ObjectId(`${request.body.info}`) }
db.collection('schidtter').deleteOne(query)
.then(result => {
console.log(`Got some delete action ${request.body.info}`)
response.json('Upvote be done, yo')
})
.catch(err => console.log(err))
})
//initiate server listening
app.listen(PORT, () => {
console.log(`Server is running on ${PORT}`)
})