-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
149 lines (129 loc) · 3.51 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
// importing express
const express = require("express");
// importing body-parser
const bodyParser = require("body-parser");
// importing mongoose
const mongoose = require("mongoose");
// importing ejs
const ejs = require("ejs");
// import dotenv
require("dotenv").config();
//Set up default mongoose connection
var mongoDB = process.env.mongooseURL;
mongoose.set("strictQuery", false);
mongoose.connect(mongoDB, { useNewUrlParser: true });
// Creating app using express
const app = express();
// Setting the view engine to ejs
app.set("view engine", "ejs");
// Setting the public folder to static
app.use(express.static("public"));
// Creating port
const port = 3000;
// using the bodyParser in urlEncoder mode
app.use(bodyParser.urlencoded({ extended: true }));
const articleSchema = {
title: String,
content: String,
};
const Article = mongoose.model("Article", articleSchema);
// get function for "/" route
app.get("/", (req, res) => {
res.send(
"<div><h1>Wiki-API</h1><h2>This API is RESTful you can use the following commands:</h2><h3>For all articles use '/articles' route:</h3><ul><li>GET</li><li>POST</li><li>DELETE</li></ul><h3>For Specific articeles use the route '/articles/{article-name}':</article-name></h3><ul><li>GET</li><li>POST</li><li>PATCH</li><li>PUT</li><li>DELETE</li></ul></div>"
);
});
// -----------------------------REQUEST FOR ALL ARTICLES-----------------------------
app
.route("/articles")
.get((req, res) => {
Article.find({}, { _id: 0, title: 1, content: 1 }, (err, articles) => {
if (!err) {
res.send(articles);
} else {
res.send(err);
}
});
})
.post((req, res) => {
const newArticle = new Article({
title: req.body.title,
content: req.body.content,
});
newArticle.save((err) => {
if (!err) {
res.send("Successfully added new article.");
} else {
res.send(err);
}
});
})
.delete((req, res) => {
Article.deleteMany((err) => {
if (!err) {
console.log("Successfully deleted all articles");
} else {
console.log(err);
}
});
});
// -----------------------------REQUEST FOR SPECIFIC ARTICLES-----------------------------
app
.route("/articles/:articleID")
.get((req, res) => {
Article.findOne(
{ title: req.params.articleID },
{ _id: 0, title: 1, content: 1 },
(err, article) => {
if (!err) {
if (article !== null) {
res.send(article);
} else {
res.send("Article Not Found");
}
} else {
res.send(err);
}
}
);
})
.put((req, res) => {
Article.updateOne(
{ title: req.params.articleID },
{ title: req.body.title, content: req.body.content },
(err) => {
if (!err) {
res.send("Updated Successfully");
} else {
res.send(err);
}
}
);
})
.patch((req, res) => {
Article.updateOne(
{ title: req.params.articleID },
{ $set: req.body },
(err) => {
if (!err) {
res.send("Sucessfully patched");
} else {
res.send(err);
}
}
);
})
.delete((req, res) => {
Article.deleteOne({ title: req.params.articleID }, (err) => {
if (!err) {
res.send("Successfully deleted");
} else {
res.send(err);
}
});
});
// Listening to the port
app.listen(port, () => {
// Console logging the server starting message
console.log("Server is online at port : " + port);
});