-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (84 loc) · 2.12 KB
/
index.js
File metadata and controls
93 lines (84 loc) · 2.12 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
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
var mongoose = require('mongoose')
var methodOverride = require('method-override')
mongoose.connect('mongodb://localhost/restfull_blog_app')
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(bodyParser.urlencoded({extended: true}))
app.use(methodOverride("_method"))
var blogSchema = new mongoose.Schema({
title: String,
image: String,
body: String,
created: { type:Date, default: Date.now}
})
var Blog = mongoose.model('Blog', blogSchema)
app.get('/', function (req, resp) {
resp.redirect('/blog')
})
// REST FULL ROUTE OF 7
app.get('/blog', function (req, resp) {
Blog.find({}, function (err, data) {
if (err) { console.log(err) }
else {
resp.render('index' , {blogs: data})
}
})
})
app.get('/blog/new', function (req,resp) {
resp.render('newblog')
})
app.post('/blog', function (req, resp) {
Blog.create(req.body.blog, function (err, data) {
if (err) { console.log(err)
resp.redirect('/blog/new') }
else {
console.log(data)
resp.redirect('/blog')
}
})
})
app.get('/blog/:id', function (req, resp) {
Blog.findById(req.params.id, function (err, data) {
if (err) { console.log(err)
resp.redirect('/blog')
}
else {
resp.render('showblog', {blog: data} )
}
})
})
app.get('/blog/:id/edit', function (req, resp) {
Blog.findById(req.params.id, function (err, data) {
if (err) { console.log(err)
resp.redirect('/blog')
}
else {
resp.render('editblog', {blog: data})
}
})
})
app.put('/blog/:id', function (req, resp) {
Blog.findByIdAndUpdate(req.params.id, req.body.blog, function (err, data){
if (err) { console.log(err)
resp.redirect('/blog')}
else {
console.log(data)
resp.redirect('/blog')
}
})
})
app.delete('/blog/:id', function (req, resp) {
Blog.findByIdAndRemove(req.params.id, function (err, data) {
if (err) { console.log(err)}
else{
console.log(data)
}
resp.redirect('/blog')
})
})
app.listen(1230, function () {
console.log('Server running in 1230')
})