-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
98 lines (83 loc) · 2.39 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
const express = require("express");
require("dotenv").config();
const app = express();
const path = require("path");
const exphbs = require("express-handlebars");
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
const bodyParser = require("body-parser");
const methodOverride = require("method-override");
const upload = require("express-fileupload");
const session = require("express-session");
const flash = require("connect-flash");
const { mongoDbUrl } = require("./config/database");
const passport = require("passport");
mongoose
.connect(mongoDbUrl, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
})
.then(db => {
console.log("MONGO DB CONNECTED");
})
.catch(error => console.log(error));
app.use(express.static(path.join(__dirname, "public")));
// Set View Engine
const {
select,
generateDate,
paginate,
} = require("./helpers/handlebars-helpers");
app.engine(
"handlebars",
exphbs({
defaultLayout: "home",
helpers: { select: select, generateDate: generateDate, paginate: paginate },
}),
);
app.set("view engine", "handlebars");
// Upload Middleware
app.use(upload());
// Body Parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Method override
app.use(methodOverride("_method"));
app.use(
session({
secret: "sudinranjitkar",
resave: true,
saveUninitialized: true,
// cookie: { secure: true },
}),
);
app.use(flash());
// PASSPORT
app.use(passport.initialize());
app.use(passport.session());
// Local variables usign middlewares
app.use((req, res, next) => {
res.locals.user = req.user || null;
res.locals.success_message = req.flash("success_message");
res.locals.error_message = req.flash("error_message");
res.locals.error = req.flash("error");
next();
});
// Load Routes
const main = require("./routes/home/index");
const mainAdmin = require("./routes/admin/main-admin");
const posts = require("./routes/admin/posts");
const categories = require("./routes/admin/categories");
const comments = require("./routes/admin/comments");
// Use Routes
app.use("/", main);
app.use("/admin", mainAdmin);
app.use("/admin/posts", posts);
app.use("/admin/categories", categories);
app.use("/admin/comments", comments);
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});