-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (119 loc) · 2.73 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
const express = require('express');
const app = express();
app.use(express.json());
const port = 3000;
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
const mongoose = require('mongoose');
//Set up default mongoose connection
var mongoDB = 'mongodb://localhost:27017/wikidb';
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true});
const articleSchema = {
title: String,
content: String
}
const Article = mongoose.model("Article", articleSchema);
app.route("/articles")
.get(function(req,res){
//console.log(req.params);
Article.find(function(err, articles){
console.log(articles);
if(articles.length > 0){
const jsonArticles = JSON.stringify(articles);
res.send(jsonArticles);
}
else{
res.send("no article found in wikidb");
}
});
})
.post(function(req,res){
const jsonObj = req.body;
Article.create(jsonObj, function(err) {
if(!err){
res.send('article created successfully!');
}
else{
//console.log("Error is" + err);
res.send(err);
}
});
})
.delete(function(req,res){
Article.deleteMany(function(err){
if (!err){
res.send("Successfully deleted all the articles in wikiDB.");
} else {
res.send(err);
}
});
});
//-------------------Specific Article--------------------
app.route("/articles/:articleId")
.get(function(req,res){
const id = req.params.articleId;
console.log(id);
Article.findOne({_id: id}, function(err,article){
console.log(article);
if(!err){
const jsonArticle = JSON.stringify(article);
res.send(jsonArticle);
}
else{
res.send("article not found in wikidb");
}
})
})
.put(function(req,res){
const id = req.params.articleId;
console.log("id ", id);
console.log("title ", req.query.title);
console.log("content ",req.body.content);
Article.updateOne(
{_id: id},
{
title: req.query.title,
content: req.body.content,
overwrite: true
},
function(err){
if (!err){
res.send("article updated successfully");
}
else{
res.send(err);
}
})
})
.patch(function(req,res){
Article.update(
{_id: req.params.articleId},{
title: req.query.title
},function (err){
if (!err){
res.send("article patched successfully");
}
else {
res.send(err);
}
});
})
.delete(function(req,res){
const id = req.params.articleId;
console.log("id ", id);
Article.deleteOne({_id: id}, function(err) {
console.log(err);
if (!err){
res.send("article deleted successfully");
}
else {
res.send(err);
}
// console.log(res);
})
});
app.listen(port, function() {
console.log("Server has started on port " + port);
});