-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
120 lines (99 loc) · 3.25 KB
/
app.js
File metadata and controls
120 lines (99 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
//This file contains the express code for creating server and routing,which is much easier.
const express = require('express');
//Morgan is a http request logger middleware
const morgan = require('morgan');
//Used to connect to mongodb
const mongoose = require('mongoose');
//Used to create an instance of schema
const Blog = require('./models/blog');
// express app
const app = express();
// connect to mongodb & listen for requests
const dbURI = "mongodb+srv://username:password@nodetuts.nk7gb.mongodb.net/nodetuts?retryWrites=true&w=majority"
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(result => app.listen(3000)) //Only listens for request after database connection
.catch(err => console.log(err));
// listen for requests
// app.listen(3000);
// register view engine
// ejs is used to render dynamic contents
// Default look at views folder
app.set('view engine', 'ejs');
// app.set('views', 'myviews');
// middleware & static files
//Browser cannot access static files such as stylesheets without the following command
//Look for static files in public folder
app.use(express.static('public'));
// This middleware is executed for every request
app.use((req, res, next) => {
console.log('new request made:');
console.log('host: ', req.hostname);
console.log('path: ', req.path);
console.log('method: ', req.method);
next();
});
app.use(morgan('dev'));
//dev is a mode of outputting the data there are others like tiny
//Next is very imp.After executing middleware the site wont load since it does not automatically move to the next code
//Explicitily mention to go on to the next block
// mongoose & mongo tests
app.get('/add-blog', (req, res) => {
const blog = new Blog({
title: 'new blog',
snippet: 'about my new blog',
body: 'more about my new blog'
})
//result the saved collection
blog.save()
.then(result => {
res.send(result);
})
.catch(err => {
console.log(err);
});
});
app.get('/all-blogs', (req, res) => {
//Method to get all the blogs
//then is a promise
Blog.find()
.then(result => {
res.send(result);
})
.catch(err => {
console.log(err);
});
});
app.get('/single-blog', (req, res) => {
Blog.findById('621bd55c7dcb41d73d759622')
.then(result => {
res.send(result);
})
.catch(err => {
console.log(err);
});
});
app.get('/', (req, res) => {
res.redirect('/blogs');
});
app.get('/about', (req, res) => {
//title parameter is passed to the view about.ejs file with value About
res.render('about', { title: 'About' });
});
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() is a middleware it is executed for every request
// Comes to this statement if and only if the previous statement is not executed and response is not sent back
app.use((req, res) => {
res.status(404).render('404', { title: '404' });
});