-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
124 lines (108 loc) · 2.81 KB
/
app.js
File metadata and controls
124 lines (108 loc) · 2.81 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
123
124
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
//
const { log } = console; /* destructure logging */
// <====>
const app = express();
app.set("view engine", "ejs"); /* set ejs as View engine */
app.use(bodyParser.urlencoded({ extended: true })); /* use bodyparser */
app.use(express.static("public")); /* set middle-ware */
// <====>
//! CONNECTION TO DATABASE STARTS
mongoose
.connect("mongodb://127.0.0.1:27017/wikiDB")
.then(function () {
log("Connection Secured!");
})
.catch(function () {
log("Something Went wrong!");
});
// CREATE COLLECTION SCHEMA
const articleSchema = new mongoose.Schema({
title: String,
content: String,
});
// CREATE SCHEMA MODEL
const Article = mongoose.model("Article", articleSchema);
//! CONNECTION TO DATABASE ENDS
//Handle ROutes to /articles using chainable route
app
.route("/articles")
//Get Mothod
.get(function (req, res) {
Article.find()
.then(function (data) {
res.send(data);
})
.catch(function (err) {
res.send(err);
});
})
//Post Method
.post(function (req, res) {
const requestedDataTitle = req.body.title;
const requestedDataContent = req.body.content;
const newArticle = new Article({
title: requestedDataTitle,
content: requestedDataContent,
});
newArticle.save().then(function () {
log("Article Saved!");
res.redirect("/articles");
});
})
// Delete Method
.delete(function (req, res) {
Article.deleteMany().then(() => {
log("Many deleted");
res.send("All Articles deleted");
});
});
app
.route("/articles/:id")
//get
.get(function (req, res) {
const ID = req.params.id; /* get params value */
Article.findOne({ title: ID })
.then(function (data) {
if (!data) {
res.send("There is no article with that title!");
} else {
res.send(data);
}
})
.catch(function (err) {
res.send(err);
});
})
//put - overwrite the whole document
.put(function (req, res) {
Article.updateOne(
{ title: req.params.id },
{ title: req.body.title, content: req.body.content },
{ upsert: true }
).then(function () {
res.send("Update was Successful");
});
})
//patch
.patch(function (req, res) {
Article.updateOne(
{ title: req.params.id },
{ $set: { title: req.body.title, content: req.body.content } }
).then(function () {
res.send("Doc updated Successfully");
});
})
//delete
.delete(function (req, res) {
Article.deleteOne({ title: req.params.id }).then(function () {
res.send("Doc Deleted");
});
});
// <====>
const PORT = 3000;
app.listen(PORT, function () {
log("Server Up and Running, Happy Coding Stroge!");
});