-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
111 lines (78 loc) · 2.58 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
105
106
107
108
109
110
111
import express from "express"
import dotenv from "dotenv"
import session from "express-session"
import cookieParser from "cookie-parser"
import { v4 as uuidv4 } from 'uuid'
import path from "path"
import { fileURLToPath } from 'url'
import hbs from "hbs"
import Handlebars from "hbs"
import passport from "passport"
import nocache from "nocache"
import user_route from "./Routers/userRoute.js"
import owner_route from "./Routers/ownerRoute.js"
import admin_route from "./Routers/adminRoute.js"
import mongodbConnection from "./config/mongodbConnection.js"
dotenv.config({ path: "config.env" })
const app = express()
const port = process.env.PORT || 3000
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
/** Middleware */
app.set("view engine", "hbs")
app.use(nocache());
app.use(express.json())
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }))
app.use("/img", express.static(path.join(__dirname, "public/img")))
app.use("/js", express.static(path.join(__dirname, "public/js")))
app.use("/plugins", express.static(path.join(__dirname, "public/plugins")))
app.use("/styles", express.static(path.join(__dirname, "public/styles")))
app.use(session({
secret: uuidv4(),
saveUninitialized: true,
resave: false
}))
app.use(passport.initialize());
app.use(passport.session());
// /////HELPER//////
hbs.registerPartials(path.join(__dirname, '/views/partials'))
hbs.registerHelper('toJson', function (data) {
return new Handlebars.SafeString(JSON.stringify(data));
});
Handlebars.registerHelper('unescape', function (options) {
const str = options.fn(this);
const entities = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
'`': '`'
};
return new Handlebars.SafeString(str.replace(/&|<|>|"|'|`/g, match => entities[match]));
});
hbs.registerHelper('dateFormat', function (date) {
const options = { year: 'numeric', month: 'short', day: 'numeric' };
return date.toLocaleDateString(undefined, options);
});
hbs.registerHelper('times', function (n, block) {
let accum = '';
for (let i = 0; i < n; i++) {
accum += block.fn(i);
}
return accum;
});
app.use("/", user_route)
app.use("/owner", owner_route)
app.use("/admin", admin_route)
app.get('*', (req, res) => {
res.render("404")
})
mongodbConnection.connect().then(() => {
try {
app.listen(port, () => console.log(`App listening on port http://localhost:${port}`))
} catch (error) {
console.log(error);
}
})