-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.js
180 lines (150 loc) · 4.44 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const express = require('express');
const router = express.Router();
const Post = require('../models/Post');
/**
* GET /
* HOME
*/
router.get('', async (req, res) => {
try {
const locals = {
title: "NodeJs Blog",
description: "Simple Blog created with NodeJs, Express & MongoDb."
}
let perPage = 10;
let page = req.query.page || 1;
const data = await Post.aggregate([ { $sort: { createdAt: -1 } } ])
.skip(perPage * page - perPage)
.limit(perPage)
.exec();
// Count is deprecated - please use countDocuments
// const count = await Post.count();
const count = await countDocuments({});
const nextPage = parseInt(page) + 1;
const hasNextPage = nextPage <= Math.ceil(count / perPage);
res.render('index', {
locals,
data,
current: page,
nextPage: hasNextPage ? nextPage : null,
currentRoute: '/'
});
} catch (error) {
console.log(error);
}
});
// router.get('', async (req, res) => {
// const locals = {
// title: "NodeJs Blog",
// description: "Simple Blog created with NodeJs, Express & MongoDb."
// }
// try {
// const data = await Post.find();
// res.render('index', { locals, data });
// } catch (error) {
// console.log(error);
// }
// });
/**
* GET /
* Post :id
*/
router.get('/post/:id', async (req, res) => {
try {
let slug = req.params.id;
const data = await Post.findById({ _id: slug });
const locals = {
title: data.title,
description: "Simple Blog created with NodeJs, Express & MongoDb.",
}
res.render('post', {
locals,
data,
currentRoute: `/post/${slug}`
});
} catch (error) {
console.log(error);
}
});
/**
* POST /
* Post - searchTerm
*/
router.post('/search', async (req, res) => {
try {
const locals = {
title: "Seach",
description: "Simple Blog created with NodeJs, Express & MongoDb."
}
let searchTerm = req.body.searchTerm;
const searchNoSpecialChar = searchTerm.replace(/[^a-zA-Z0-9 ]/g, "")
const data = await Post.find({
$or: [
{ title: { $regex: new RegExp(searchNoSpecialChar, 'i') }},
{ body: { $regex: new RegExp(searchNoSpecialChar, 'i') }}
]
});
res.render("search", {
data,
locals,
currentRoute: '/'
});
} catch (error) {
console.log(error);
}
});
/**
* GET /
* About
*/
router.get('/about', (req, res) => {
res.render('about', {
currentRoute: '/about'
});
});
// function insertPostData () {
// Post.insertMany([
// {
// title: "Building APIs with Node.js",
// body: "Learn how to use Node.js to build RESTful APIs using frameworks like Express.js"
// },
// {
// title: "Deployment of Node.js applications",
// body: "Understand the different ways to deploy your Node.js applications, including on-premises, cloud, and container environments..."
// },
// {
// title: "Authentication and Authorization in Node.js",
// body: "Learn how to add authentication and authorization to your Node.js web applications using Passport.js or other authentication libraries."
// },
// {
// title: "Understand how to work with MongoDB and Mongoose",
// body: "Understand how to work with MongoDB and Mongoose, an Object Data Modeling (ODM) library, in Node.js applications."
// },
// {
// title: "build real-time, event-driven applications in Node.js",
// body: "Socket.io: Learn how to use Socket.io to build real-time, event-driven applications in Node.js."
// },
// {
// title: "Discover how to use Express.js",
// body: "Discover how to use Express.js, a popular Node.js web framework, to build web applications."
// },
// {
// title: "Asynchronous Programming with Node.js",
// body: "Asynchronous Programming with Node.js: Explore the asynchronous nature of Node.js and how it allows for non-blocking I/O operations."
// },
// {
// title: "Learn the basics of Node.js and its architecture",
// body: "Learn the basics of Node.js and its architecture, how it works, and why it is popular among developers."
// },
// {
// title: "NodeJs Limiting Network Traffic",
// body: "Learn how to limit netowrk traffic."
// },
// {
// title: "Learn Morgan - HTTP Request logger for NodeJs",
// body: "Learn Morgan."
// },
// ])
// }
// insertPostData();
module.exports = router;