-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (74 loc) · 2.16 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
// * THIRD-PARTY MODULES
const express = require("express");
const passport = require("passport");
const nunjucks = require("nunjucks");
const dateFilter = require("nunjucks-date-filter");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const MongoDBSession = require("connect-mongodb-session")(session);
// * CORE MODULES
const path = require("path");
// * MY MODULES
const UserModel = require("./models/User");
const dotenv = require("dotenv");
const db = require("./config/database");
// # Routes
const pageRoutes = require("./routes/pageRoutes");
const authRoutes = require("./routes/authRoutes");
const linkRoutes = require("./routes/linkRoutes");
const app = express();
dotenv.config();
// ! TEMPLATE ENGINE
app.engine("html", nunjucks.render);
app.set("view engine", "html");
const configNjk = nunjucks.configure("views", {
autoescape: true,
noCache: true,
express: app,
});
configNjk.addFilter("date", dateFilter);
// Şablonların ve statik dosyaların konumunu bildirme
app.set("views", path.join(__dirname, "views"));
app.set("public", path.join(__dirname, "public"));
app.set("publicPath", "/public");
// ! MONGO STORE
const store = new MongoDBSession({
uri: process.env.MONGO_URI,
collection: "sessions",
});
// ! MIDDLEWARES
app.use(express.static("public"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(
session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: true,
store: store,
})
);
// Need to require the entire Passport config module so app.js knows about it
require("./config/passport");
app.use(passport.initialize());
app.use(passport.session());
app.use(async (req, res, next) => {
res.locals.user = req.isAuthenticated() ? req.user : null;
next();
});
// ! ROUTES
app.use(pageRoutes);
app.use(authRoutes);
app.use(linkRoutes);
// 404 Not Found middleware
app.use((req, res, next) => {
res.send("404 Not Found");
});
// RUN LISTEN SERVER
const port = process.env.PORT || 3000;
db();
app.listen(port, () => {
console.log(`Server is running on ${port} Port`);
});