-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
121 lines (98 loc) · 2.61 KB
/
Copy pathapp.js
File metadata and controls
121 lines (98 loc) · 2.61 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
const express = require('express');
const mongoose = require('mongoose')
const bodyParser = require('body-parser');
const app = express();
mongoose.connect('mongodb://localhost:27017/RestDB');
app.use(bodyParser.urlencoded({extended:true}));
const ApiSchema = new mongoose.Schema({
title:String,
description:String
})
const ApiModel = new mongoose.model('Api',ApiSchema);
app.get('/',(req,res)=>{
res.sendFile(__dirname+'\\index.html')
})
app.route('/api/jokes')
.get((req,res)=>{
ApiModel.find((err,result)=>{
if(!err){
if(result.length === 0){
res.send("OOps there is no data to show Please add data and try again..")
}else{
res.send(result)
}
}
else{
res.send("oops something fishy...")
}
})
})
.post((req,res)=>{
const Api1 = new ApiModel({
title: req.body.title,
description: req.body.description
})
Api1.save((err,result)=>{
if(!err){
res.send('Successfully added to Database...')
}
})
})
.delete((req,res)=>{
ApiModel.deleteMany({},(err)=>{
if(!err){
res.send("Successfully deleted from Database....")
}
})
});
app.route('/api/jokes/:topic')
.get((req,res)=>{
const parameter = req.params.topic;
ApiModel.findOne({title:parameter},(err,result)=>{
if(result){
res.send(result);
}
else{
res.send("Not Found");
}
})
})
.put((req,res)=>{
ApiModel.findOneAndUpdate({title:req.params.topic},
{title:req.body.title,description:req.body.description},
{overwrite:true},
(err)=>{
if(!err){
res.send("Successfully Updated ....")
}
else{
res.send("!something went wrong Please try again later...")
}
}
)
})
.patch((req,res)=>{
ApiModel.findOneAndUpdate({title:req.params.topic},
{$set:req.body},
(err,result)=>{
if(!err){
res.send("Successfully Updated ....")
}
else{
res.send("!something went wrong Please try again later...")
}
})
})
.delete((req,res)=>{
ApiModel.deleteOne({title:req.params.topic},(err)=>{
if(!err){
res.send("Successfully Deleted From Database...")
}
else{
res.send("!something went wrong Please try again later...")
}
})
})
app.listen(3000,()=>{
console.log('server started on port 3000');
})