-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathapp.js
More file actions
123 lines (107 loc) · 3.22 KB
/
app.js
File metadata and controls
123 lines (107 loc) · 3.22 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
119
120
121
122
const express = require('express');
const bodyParser = require("body-parser");
const path = require('path');
const Joi = require('joi');
const db = require("./db");
const collection = "todo";
const app = express();
// schema used for data validation for our todo document
const schema = Joi.object().keys({
todo : Joi.string().required()
});
// parses json data sent to us by the user
app.use(bodyParser.json());
// serve static html file to user
app.get('/',(req,res)=>{
res.sendFile(path.join(__dirname,'index.html'));
});
// read
app.get('/getTodos',(req,res)=>{
// get all Todo documents within our todo collection
// send back to user as json
db.getDB().collection(collection).find({}).toArray((err,documents)=>{
if(err)
console.log(err);
else{
res.json(documents);
}
});
});
// update
app.put('/:id',(req,res)=>{
// Primary Key of Todo Document we wish to update
const todoID = req.params.id;
// Document used to update
const userInput = req.body;
// Find Document By ID and Update
db.getDB().collection(collection).findOneAndUpdate({_id : db.getPrimaryKey(todoID)},{$set : {todo : userInput.todo}},{returnOriginal : false},(err,result)=>{
if(err)
console.log(err);
else{
res.json(result);
}
});
});
//create
app.post('/',(req,res,next)=>{
// Document to be inserted
const userInput = req.body;
// Validate document
// If document is invalid pass to error middleware
// else insert document within todo collection
Joi.validate(userInput,schema,(err,result)=>{
if(err){
const error = new Error("Invalid Input");
error.status = 400;
next(error);
}
else{
db.getDB().collection(collection).insertOne(userInput,(err,result)=>{
if(err){
const error = new Error("Failed to insert Todo Document");
error.status = 400;
next(error);
}
else
res.json({result : result, document : result.ops[0],msg : "Successfully inserted Todo!!!",error : null});
});
}
})
});
//delete
app.delete('/:id',(req,res)=>{
// Primary Key of Todo Document
const todoID = req.params.id;
// Find Document By ID and delete document from record
db.getDB().collection(collection).findOneAndDelete({_id : db.getPrimaryKey(todoID)},(err,result)=>{
if(err)
console.log(err);
else
res.json(result);
});
});
// Middleware for handling Error
// Sends Error Response Back to User
app.use((err,req,res,next)=>{
res.status(err.status).json({
error : {
message : err.message
}
});
})
db.connect((err)=>{
// If err unable to connect to database
// End application
if(err){
console.log('unable to connect to database');
process.exit(1);
}
// Successfully connected to database
// Start up our Express Application
// And listen for Request
else{
app.listen(3000,()=>{
console.log('connected to database, app listening on port 3000');
});
}
});