-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
200 lines (174 loc) · 6.23 KB
/
app.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import ejs, { render } from "ejs"
import bodyParser from "body-parser";
import mongoose from "mongoose"
import express from "express";
import dotenv from "dotenv"
dotenv.config()
const app = express();
const port = 3000;
app.set('view engine', 'ejs')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static("public"));
//const url = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.${process.env.DB_CLUSTERNAME}.${process.env.DB_HOST}/wikiDB`;
const url = "mongodb://127.0.0.1:27017/wikiDB";
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
const articleSchema = new mongoose.Schema({
title: String,
content: String
})
const Article = new mongoose.model("article", articleSchema);
app.get("/", function (req, res) {
res.render("index.ejs");
})
/////////////////////////Request Targeting all Article\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
app.route("/articles")
.get(function (req, res) {
Article.find({})
.then(function (foundArticles) {
if (foundArticles) {
res.send(foundArticles);
}
})
.catch(function (err) {
res.send(err);
})
})
.post(function (req, res) {
const article = new Article({
title: req.body.title,
content: req.body.content
});
article.save();
res.send(req.body);
})
.delete(function (req, res) {
Article.deleteMany({})
.then(function () {
console.log("Delete All Articles Successfully! ")
})
.catch(function (err) {
res.send(err);
})
});
/////////////////////////Request Targeting A Specific Article\\\\\\\\\\\\\\\\\\\\\\\\
app.route("/articles/:articleTitle")
.get(function (req, res) {
Article.findOne({ title: req.params.articleTitle })
.then(function (foundArticle) {
if (foundArticle) {
res.send(foundArticle);
} else {
res.send("No articles matching that title was found. ")
}
})
.catch(function (err) {
res.send(err);
})
})
.put(function (req, res) {
Article.updateOne(
{ title: req.params.articleTitle },
{ title: req.body.title, content: req.body.content }
)
.then(function (foundArticle) {
if (foundArticle.matchedCount == 1) {
res.send("foundArticle and Update it");
} else {
res.send("No articles matching that title was found. ")
}
})
.catch(function (err) {
res.send(err);
})
})
.patch(function (req, res) {
Article.updateOne(
{ title: req.params.articleTitle },
{ $set: req.body }
)
.then(function (foundArticle) {
if (foundArticle.matchedCount == 1) {
res.send("foundArticle and Update it");
} else {
res.send("No articles matching that title was found. ")
}
})
.catch(function (err) {
res.send(err);
})
})
.delete(function (req, res) {
Article.deleteOne(
{ title: req.params.articleTitle },
)
.then(function (foundArticle) {
if (foundArticle.deletedCount == 1) {
res.send("foundArticle and Delete it");
} else {
res.send("No articles matching that title was found. ")
}
})
.catch(function (err) {
res.send(err);
})
})
app.listen(port, function () {
console.log(`Server is running on port ${port}`)
})
// app.get("/articles", function (req, res) {
// Article.find({})
// .then(function (foundArticles) {
// if (foundArticles) {
// res.send(foundArticles);
// // res.render("index.ejs");
// }
// })
// .catch(function (err) {
// res.send(err);
// })
// })
// app.post("/articles", function (req, res) {
// const article = new Article({
// title: req.body.title,
// content: req.body.content
// });
// article.save();
// res.send(req.body);
// })
// app.delete("/articles", function (req, res) {
// Article.deleteMany({})
// .then(function () {
// console.log("Delete All Articles Successfully! ")
// })
// .catch(function (err) {
// res.send(err);
// })
// })
// [
// {
// "_id": "64d73ad85c9b8572965c7922",
// "title": "REST",
// "content": "REST is short for REpresenaional state Transfer"
// },
// {
// "_id": "5c139771d79ac8eac11e754a",
// "title": "API",
// "content": "API stands for Application Programming Interface. It is a set of subroutine definitions, communication protocols, and tools for building software. In general terms, it is a set of clearly defined methods of communication among various components. A good API makes it easier to develop a computer program by providing all the building blocks, which are then put together by the programmer."
// },
// {
// "_id": "5c1398ecd79ac8eac11e7567",
// "title": "DOM",
// "content": "The Document Object Model is like an API for interacting with our HTML"
// },
// {
// "_id": "5c1398aad79ac8eac11e7561",
// "title": "Bootstrap",
// "content": "This is a framework developed by Twitter that contains pre-made front-end templates for web design"
// },
// {
// "_id": "64d762bab91c960a44ff149c",
// "title": "jack Bauer",
// "content": "There are two hands that can beat a royal flush. Jack Bauer's right hand and Jack Bauer's left hand. 69 7.52\n",
// "__v": 0
// }
// ]