-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (58 loc) · 2.06 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
const express = require("express")//require express to help handle API quickly
const app = express()
const MongoClient = require("mongodb").MongoClient //require mongodb to talk to the database
const {v4:uuidv4} = require("uuid")
require("dotenv").config()
app.set("view engine","ejs") // set the view engine using ejs
app.use(express.static("public"))
app.use(express.urlencoded({extended:true})) // parsing url to js object
app.use(express.json()) // teach express to read json
let db,
dbConnectionString = process.env.DB_STR,
dbName = "Todos"
MongoClient.connect(dbConnectionString,{useUnifiedTopology: true})
.then(client =>{
console.log('connected to Database')
db = client.db(dbName) // connect to our database named "Todos"
})
//create task
app.post('/createTask',async (request, response)=>{
try{
const result = await db.collection("Tasks").insertOne({
id: uuidv4(),
taskName: request.body.taskName,
finished: false,
recordTime: new Date()
})
console.log("post added")
response.redirect("/")
}catch(err){console.err(err)}
})
//get task
app.get('/', async (request, response)=>{
try{
const data = await db.collection("Tasks").find().sort({finished: 1, recordTime: 1}).toArray()
response.render('index.ejs', {posts:data})
}catch(err){console.err(err)}
})
//delete task
app.delete('/deleteTask', async (request, response)=>{
const result = await db.collection('Tasks').deleteOne({id:request.body.deleteId})
console.log('data deleted')
response.json('data deleted')
})
//update task
app.put('/updateCompleteness', async (request, response)=>{
const result = await db.collection('Tasks').updateOne({id:request.body.updateId},{
$set:{
finished: !request.body.completeness
}
},{
sort: {finished: -1, recordTime: 1},
upsert: false
})
console.log('completeness updated')
response.json("completeness updated")
})
//Listening to the port
app.listen(process.env.PORT, ()=>console.log('server\'s running' ))