-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
85 lines (76 loc) · 2.01 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
/**
* Node Modules.
*/
const express = require("express");
const path = require("path");
const cors = require("cors");
const ejsMate = require("ejs-mate");
const methodOverride = require("method-override");
const morgan = require("morgan");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const flash = require("connect-flash");
/**
* Configs.
*/
const { sessionConfig } = require("./configs/sessionConfig.js");
/**
* Utils.
*/
const connectDB = require("./utils/db/conncetDB.js");
const ServerError = require("./utils/ServerError.js");
const currentUser = require("./utils/user/currentUser.js");
/**
* Routers.
*/
const UserRouter = require("./routes/user/user.router.js");
const AdminRouter = require("./routes/admin/admin.router.js");
const ContributionRouter = require("./routes/contribution/contribution.router.js");
const FileRouter = require("./routes/file/file.router.js");
/**
* Declarations.
*/
const app = express();
const PORT = process.env.PORT || 3001;
/**
* Server Middlewares.
*/
app.engine("ejs", ejsMate);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
app.use(express.static(path.join(__dirname, "/public")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
app.use(methodOverride("_method"));
app.use(cookieParser(process.env.SIGN_COOKIE));
app.use(session(sessionConfig));
app.use(morgan("dev"));
app.use(flash());
/**
* Global res variables.
*/
app.use(async (req, res, next) => {
res.locals.currentUser = await currentUser(req, res);
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
next();
});
/**
* Routes.
*/
app.use("/", UserRouter);
app.use("/", AdminRouter);
app.use("/", ContributionRouter);
app.use("/", FileRouter)
/**
* Home Route.
*/
app.route("/")
.get((req, res)=>{
res.render("home");
});
app.listen(PORT, ()=>{
connectDB();
console.log(`${process.env.NODE_ENV} server running on port ${PORT}`);
})