-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
152 lines (135 loc) · 4.37 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
150
151
152
// mkdir RESTfulBlogApp
// cd RESTfulBlogApp
// npm init
// git init
// hub create
// npm install express mongoose body-parser ejs --save
const bodyParser = require('body-parser'),
methodOverride = require('method-override'), // npm install method-override --save
expressSanitizer = require('express-sanitizer'), // npm install express-sanitizer --save
mongoose = require('mongoose'),
express = require('express'),
app = express();
// MONGOOSE CONFIG
mongoose.connect('mongodb://localhost:27017/restful_blog_app', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Connected to DB!'))
.catch(error => console.log(error.message));
// MONGOOSE/MODEL CONFIG
const blogSchema = new mongoose.Schema({
title: String,
image: String,
body: String,
created: { type: Date, default: Date.now }
});
const Blog = mongoose.model('Blog', blogSchema);
// Blog.create({
// title: 'Test Blog',
// image: 'https://images.unsplash.com/photo-1586088209375-7c9f50ff8b5a?ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80',
// body: 'This is a blog post',
// });
// APP CONFIG
app.set('view engine', 'ejs'); // ejs template engine
app.use(express.static('public')); // static files
app.use(bodyParser.urlencoded({extended: true})); // body parser
app.use(methodOverride('_method')); // method-override put
app.use(expressSanitizer()); // removes script. we should use it after body parser
// RESTFUL ROUTES
// home
app.get('/', function(req, res) {
res.redirect('/blogs');
});
// index
app.get('/blogs', function(req, res) {
Blog.find({}, function(err, blogs) {
if (err) {
console.log(err);
} else {
res.render('index', { blogs: blogs });
}
});
});
// new
app.get('/blogs/new', function(req, res) {
res.render('new');
});
// create
app.post('/blogs', function(req, res) {
console.log(req.body); // blog: { title: 'Test Blog', image: 'https://images.unsplash.com/photo-1586088209375-7c9f50ff8b5a?ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80', body: "<h1>blog</h1>\r\n<script>alert('hey');</script>"
req.body.blog.body = req.sanitize(req.body.blog.body);
console.log(req.body); // blog: { title: 'Test Blog', image: 'https://images.unsplash.com/photo-1586088209375-7c9f50ff8b5a?ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80', body: '<h1>blog</h1>\r\n'
// create blog
// Blog.create(data, callback);
Blog.create(req.body.blog, function(err, newBlog) {
if (err) {
res.render('new');
} else {
// redirect to index
res.redirect('/blogs');
}
});
});
// show
app.get('/blogs/:id', function(req, res) {
// find blog by id
Blog.findById(req.params.id, function(err, foundBlog) {
if (err) {
res.redirect('/blogs');
} else {
// render blog
// res.send('show page');
res.render('show', { blog: foundBlog });
}
});
});
// edit
app.get('/blogs/:id/edit', function(req, res) {
// find blog
Blog.findById(req.params.id, function(err, foundBlog) {
if (err) {
res.redirect('/blogs');
} else {
// render edited template
res.render('edit', { blog: foundBlog });
}
});
});
// update
app.put('/blogs/:id', function(req, res) {
req.body.blog.body = req.sanitize(req.body.blog.body);
// find blog by id
// Blog.findByIdAndUpdate(id, newData, callback)
Blog.findByIdAndUpdate(req.params.id, req.body.blog, function(err, updatedBlog) {
// req.body.form because name="body[]" in the edit form
console.log(req.body.blog); // { title: 'Test Blog', image: 'https://images.unsplash.com/photo-1586088209375-7c9f50ff8b5a?ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80', body: 'This is a blog post' }
console.log(req.params.id); // 5f0d46dce72cbc3c96cf5bc7
if (err) {
res.redirect('/blogs');
} else {
// render updated blog
// res.send('update');
res.redirect('/blogs/' + req.params.id);
// or
// res.redirect('/blogs/' + updatedBlog.id);
}
});
});
// destroy
app.delete('/blogs/:id', function(req, res) {
// destroy blog
Blog.findByIdAndRemove(req.params.id, function(err) {
if (err) {
res.redirect('/blogs');
} else {
// redirect somewhere
res.redirect('/blogs');
}
});
// res.send('you have reached the destroy route');
});
// PORT CONFIG
app.listen(process.env.PORT || 3000, process.env.IP, function() {
console.log('The blog server has started');
});