-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
155 lines (146 loc) · 4 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
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
// Imports
const express = require("express");
const handlebars = require("express-handlebars");
const bodyParser = require("body-parser");
const app = express();
const admin = require("./routes/admin");
const user = require("./routes/user");
const path = require("path");
const mongoose = require("mongoose");
const session = require("express-session");
const flash = require("connect-flash");
require("./models/Post");
const Post = mongoose.model("posts");
require("./models/Category");
const Category = mongoose.model("categories");
const passport = require("passport");
require("./config/auth")(passport);
const db = require("./config/db");
const version = require("./package.json").version;
// const i18n = require('./i18n')
// Config
// i18n
// app.use(i18n)
// Session
app.use(
session({
secret: "mydearsecret",
resave: true,
saveUninitialized: true
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
// Middleware
app.use((req, res, next) => {
res.locals.success_message = req.flash("success_message");
res.locals.error_message = req.flash("error_message");
res.locals.error = req.flash("error");
res.locals.user = req.user || null;
next();
});
// Body Parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Handlebars
app.engine("handlebars", handlebars({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
// Mongoose
mongoose.Promise = global.Promise;
mongoose
.connect(db.mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log("Mongonected!");
})
.catch(error => {
console.log(
"OH NO DATABASE HAS EXPLODED! because of\n============================================\n" +
error +
"\n============================================"
);
});
// Public
app.use(express.static(path.join(__dirname, "public")));
// Routes
app.get("/", (req, res) => {
Post.find()
.populate("category")
.populate("user")
.sort({ date: "DESC" })
.then(posts => {
res.render("index", { posts: posts });
})
.catch(error => {
req.flash("error_message", "Whoops, Something gone wrong..." + error);
res.redirect("/404");
});
});
app.get("/post/:slug", (req, res) => {
Post.findOne({ slug: req.params.slug })
.populate("user")
.then(post => {
if (post) {
res.render("post/index", { post: post });
} else {
req.flash("error_message", "Are you sure that post exists?");
res.redirect("/");
}
})
.catch(error => {
req.flash(
"error_msg",
"The world is changing... and something gone wrong! "
);
res.redirect("/");
});
});
app.get("/404", (req, res) => {
res.send("Oh! you found a 404!");
});
app.use("/admin", admin);
app.use("/user", user);
app.get("/categories", (req, res) => {
Category.find()
.then(categories => {
res.render("categories/index", { categories: categories });
})
.catch(error => {
req.flash("error_message", "Thank you for this beautiful ERROR!");
res.redirect("/");
});
});
app.get("/categories/:slug", (req, res) => {
Category.findOne({ slug: req.params.slug })
.then(category => {
if (category) {
Post.find({ category: category._id })
.then(posts => {
res.render("categories/posts", {
posts: posts,
category: category
});
})
.catch(error => {
req.flash("error_message", "Error when listing posts...");
res.redirect("/");
});
} else {
req.flash("error_message", "This category doesn't exist");
res.redirect("/");
}
})
.catch(error => {
req.flash("error_message", "Another one to the account!");
console.log(error);
res.redirect("/");
});
});
app.get("posts", (req, res) => {
res.send("Posts");
});
// Other
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log("Server Up!");
});