-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
104 lines (89 loc) · 2.73 KB
/
server.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
const express = require("express");
const dotEnv = require("dotenv");
const methodOverride = require("method-override");
const mongoose = require("mongoose");
const connnectDB = require("./configuration/database");
const handlebars = require("express-handlebars");
const path = require("path");
const passport = require("passport");
const session = require("express-session");
const MongoStore = require("connect-mongo");
//configuration
dotEnv.config({ path: "./configuration/config.env" });
//passport config for authentication
require("./configuration/auth")(passport);
//connnecting to mongoDB
connnectDB();
//--------------------------------- app ----------------------------------------
//init app
const app = express();
//body parser
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(
methodOverride(function(req, res) {
if (req.body && typeof req.body === "object" && "_method" in req.body) {
// look in urlencoded POST bodies and delete it
let method = req.body._method;
delete req.body._method;
return method;
}
})
);
//Static Folder
app.use(express.static(path.join(__dirname, "static")));
/* app.use(express.static(path.join(__dirname, "static", "uploads"))); */
//handlebars helpers
const {
formatDate,
truncate,
editButton,
select,
stars,
} = require("./helpers/handlebars");
//Handlebars
app.engine(
"handlebars",
handlebars({
defaultLayout: "index",
helpers: {
formatDate,
truncate,
editButton,
select,
stars,
},
})
);
app.set("view engine", "handlebars");
//session middle ware ----!!always put above passport middleware
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: false, //which means dont create a sessions until something is stores
store: MongoStore.create({
mongoUrl: process.env.MONGO_URL,
}),
})
);
//passport-middleware
app.use(passport.initialize());
app.use(passport.session());
//global variables
app.use(function(req, res, next) {
res.locals.user = req.user || null; //access user within handlebar template(not bookUser but logged in user)
next();
});
//Using the routes(routes at bottom)
app.use("/", require("./routes/index"));
app.use("/auth", require("./routes/auth_route"));
app.use("/books", require("./routes/books_route"));
app.use("/search", require("./routes/search"));
app.use("/singlebook", require("./routes/singlebook"));
app.use("/user", require("./routes/user"));
const port = process.env.PORT || 4000;
app.listen(
port,
console.log(`server running at PORT:${port}, mode : ${process.env.NODE_ENV}`)
);