-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
105 lines (97 loc) · 1.97 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
const express = require('express')
const app = express()
const mongoose = require('mongoose');
const dbURI = 'mongodb+srv://spyder:[email protected]/databasename?retryWrites=true&w=majority';
const Blog = require('./models/blog')
const methodOverride = require('method-override');
app.use(methodOverride('_method'));
app.set('view engine' ,'ejs');
app.use(express.static('public'));
app.use(express.urlencoded({extended:true}));
mongoose.connect(dbURI).then((result)=>
{
app.listen(3000);
})
.catch((err)=>{
console.log(err);
});
app.get('/about', function (req, res) {
Blog.find().then((result)=>
{
res.render('about',{home:'blog',blog:result});
})
})
app.get('/add-blog' , (req,res)=>
{
const blog = new Blog({
title: 'my new blog',
snippet:'This is my newer first blog',
body:'Hello everyone this is my first ever blog'
});
blog.save().then((result)=>
{
res.send(result);
})
;
});
app.get('/all-blogs',(req,res)=>{
Blog.find().then((result)=>
{
res.send(result);
})
.catch((err)=>{
console.log(err);
})
});
app.get('/findby-id',(req,res)=>
{
Blog.findById("6153200c95b99d89b8cd8030")
.then((result)=>
{
res.send(result);
})
.catch((err)=>
{
console.log(err);
})
});
app.get('/blog/create', function (req, res) {
res.render('create');
})
app.post("/blogs",(req,res)=>
{
const blog = new Blog(req.body);
blog.save()
.then((result)=>
{
res.redirect('/about');
})
.catch((err)=>{
console.log(err);
});
});
app.get('/blogs/:id',(req,res)=>{
var id = req.params.id;
Blog.findById(id)
.then((result)=>
{
res.render('show',{blog:result});
})
.catch((err)=>
{
console.log(err);
})
})
app.delete('/blogs/:id',(req,res)=>
{
var id = req.params.id;
Blog.findByIdAndDelete(id)
.then((result)=>
{
res.redirect('/about');
})
.catch((err)=>
{
console.log(err);
})
});