-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
81 lines (68 loc) · 1.72 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
const express = require('express');
const mongoose = require('mongoose');
const Blog = require('./models/blog');
// express app
const app = express();
// connect to mongodb & listen for requests
const dbURI='mongodb+srv://Mohamed:[email protected]/node-notes?retryWrites=true&w=majority'
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(result => app.listen(3000))
.catch(err => console.log(err));
// register view engine
app.set('view engine', 'ejs');
// mongoose & mongo tests
app.get('/add-blog', (req, res) => {
const blog = new Blog({
author: 'Mohamed AbdelAziz Elahl',
age: 25,
email: '[email protected]'
})
blog.save()
.then(result => {
res.send(result);
})
.catch(err => {
console.log(err);
});
});
app.get('/all-blogs', (req, res) => {
Blog.find()
.then(result => {
res.send(result);
})
.catch(err => {
console.log(err);
});
});
app.get('/single-blog', (req, res) => {
Blog.findById('6060aadf47ad4f2904a8e17f')
.then(result => {
res.send(result);
})
.catch(err => {
console.log(err);
});
});
app.get('/', (req, res) => {
res.redirect('/blogs');
});
app.get('/about', (req, res) => {
res.render('about', { title: 'About' });
});
// blog routes
app.get('/blogs/create', (req, res) => {
res.render('create', { title: 'Create a new blog' });
});
app.get('/blogs', (req, res) => {
Blog.find().sort({ createdAt: -1 })
.then(result => {
res.render('index', { blogs: result, title: 'All blogs' });
})
.catch(err => {
console.log(err);
});
});
// 404 page
app.use((req, res) => {
res.status(404).render('404', { title: '404' });
});