-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
158 lines (111 loc) · 3.25 KB
/
app.js
File metadata and controls
158 lines (111 loc) · 3.25 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
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
var bodyParser = require("body-parser"),
methodOverride = require("method-override"),
mongoose = require("mongoose"),
express = require("express"),
app = express();
mongoose.connect('mongodb://localhost:27017/myapp', { useUnifiedTopology: true, useNewUrlParser: true });
//this is app config
app.set("view engine", "ejs");
app.use((express.static("public")));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride("_method"));
// mongoose.set('useFindAndModify', false);
// mongoose.set('useFindAndModify', false);
// make a schema the compile
var blogSchema = new mongoose.Schema({
title: String,
image: String,
body: String,
created: { type: Date, default: Date.now }
});
//compile into an model
var Blog = mongoose.model("Blog", blogSchema);
// restful routes
// Blog.create({
// title: "my name is aman",
// body: "my height is feet and weight 75 kgs",
// image: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ_nFciUIai-C2R3WkaT19f9yr0wWjS7-d0t_P5adO42mXJWzdQEg",
// date: Date.now
// }, function(err, blogdata) {
// if (err) {
// console.log("OOPS there's an error");
// } else {
// console.log(blogdata);
// }
// });
// index blogs
app.get("/", function(req, res) {
res.redirect("/blogs");
});
app.get("/blogs", function(req, res) {
Blog.find({}, function(err, blog) {
if (err) {
console.log("OOPS there's an error");
} else {
res.render("index.ejs", { blog: blog });
}
});
});
// new blog
app.get("/blogs/new", function(err, res) {
res.render("new");
});
// create blog
app.post("/blogs", function(req, res) {
var blog = req.body.blog;
Blog.create(blog, function(err, data) {
if (err) {
console.log("theres an error in post");
} else {
console.log(data.title);
res.redirect("/blogs");
}
});
});
// show one particular blog
app.get("/blogs/:id", function(req, res) {
Blog.findById(req.params.id, function(err, data) {
if (err) {
res.redirect("/blogs");
} else {
res.render("show", { blog: data });
}
})
});
//edit form of the blog
app.get("/blogs/:id/edit", function(req, res) {
Blog.findById(req.params.id, function(err, data) {
if (err) {
console.log(err);
} else {
res.render("edit", { blog: data });
}
})
});
// after editing in the form update it
app.put("/blogs/:id", function(req, res) {
console.log(req.body.blog);
console.log(req.params.id);
Blog.updateOne({ _id: req.params.id }, req.body.blog, function(err, updatedBlog) {
console.log("Am i in");
if (err) {
//console.log(req.body.blog);
res.redirect("/blogs");
} else {
res.redirect("/blogs/" + req.params.id);
}
})
});
// delete the blog
app.delete("/blogs/:id", function(req, res) {
Blog.findByIdAndRemove(req.params.id, function(err) {
if (err) {
res.redirect("/blogs");
} else {
res.redirect("/blogs");
}
})
})
app.listen(5000, function() {
console.log(" Jai shree Ram! Server has been started");
});