-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
86 lines (77 loc) · 2.27 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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const app = express();
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
const mongoURL = 'mongodb+srv://admin-shubham:' + process.env.PASSWORD + '@cluster0.hkzz7.mongodb.net/wikiDB';
mongoose.connect(mongoURL);
const articleSchema = new mongoose.Schema({
title: String,
content: String
});
const Article = mongoose.model('Article', articleSchema);
app.route('/articles')
.get((req, res) => {
Article.find((err, foundArticles) => {
if (!err) {
res.send(foundArticles);
}
});
})
.post((req, res) => {
const newArticle = new Article({
title: req.body.title,
content: req.body.content
});
newArticle.save((err) => {
if (!err) {
res.send("successfully saved");
}
});
})
.delete((req, res) => {
Article.deleteMany({}, (err) => {
if (!err) {
res.send("successfully deleted");
}
});
});
app.route("/articles/:articleTitle")
.get((req, res) => {
Article.find({ title: req.params.articleTitle }, (err, foundArticles) => {
if (!err) {
res.send(foundArticles);
}
});
})
.put((req, res) => {
Article.replaceOne({ title: req.params.articleTitle }, {
title: req.body.title,
content: req.body.content
}, (err) => {
if (!err) {
res.send("successfully updated");
} else {
res.send("failed" + err);
}
});
})
.patch((req, res) => {
Article.updateOne({ title: req.params.articleTitle }, { $set: req.body }, (err) => {
if (!err) {
res.send("successfully updated");
}
});
})
.delete((req, res) => {
Article.deleteOne({ title: req.params.articleTitle }, (err) => {
if (!err) {
res.send("Successfully deleted");
}
});
});
app.listen(process.env.PORT || 3000, () => {
console.log("server is running on port 3000");
});