-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
96 lines (78 loc) · 2.74 KB
/
main.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
const dotenv = require("dotenv");
dotenv.config();
const jwt = require("jsonwebtoken");
const express = require("express");
//const morgan = require("morgan"); //추가적인 로그 볼수있게
const cookieParser = require("cookie-parser");
const session = require("express-session");
const path = require("path");
const app = express();
const bodyParser = require("body-parser"); //post에서 body 받기
const adminControl = require("./controllers/adminController");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser(process.env.COOKIE_SECRET_KEY));
const { swaggerUi, specs } = require("./swagger/swagger");
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(specs));
app.use((req, res, next) => {
try {
const decoded = jwt.verify(
req.cookies.authToken,
process.env.JWT_SECRET_KEY
);
if (decoded.userId == null)
return res.render('notFound.ejs', {message: "로그인이 필요합니다"});
res.locals.auth = decoded.userId;
res.locals.userType = req.cookies.userType;
res.locals.is_admin = adminControl.getAdmin(decoded.userId);
} catch (error) {
res.locals.auth = "";
res.locals.userType = "";
res.locals.is_admin = "";
}
next();
})
const loginCheck = (req, res, next) => {
if(req.cookies.authToken == undefined) {
return res.render('notFound.ejs', {message: "로그인이 필요합니다"});
}
next();
}
//Routers
const indexRouter = require("./routers/index"),
residentRouter = require("./routers/residentRouter"),
agentRouter = require("./routers/agentRouter.js"),
reviewRouter = require("./routers/reviewRouter.js"),
authRouter = require("./routers/authRouter.js"),
searchRouter = require("./routers/searchRouter"),
realtorRouter = require("./routers/realtorRouter"),
adminRouter = require("./routers/adminRouter");
//View
const layouts = require("express-ejs-layouts");
app.set("view engine", "ejs");
app.use(layouts);
app.set("port", process.env.PORT || 3000);
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
/**
* @path {GET} http://localhost:3000/
* @description 요청 데이터 값이 없고 반환 값이 있는 GET Method
*/
app.use("/", indexRouter);
app.use("/resident", loginCheck, residentRouter);
//검색페이지 접근
app.use("/search", searchRouter);
//공인중개사 페이지 접근
app.use("/agent", agentRouter);
//후기 접근
app.use("/review", reviewRouter);
//입주민이 보는 공인중개사 페이지 접근
app.use("/realtor", realtorRouter);
// Auth
app.use("/auth", authRouter);
// 관리자
app.use("/admin", adminRouter);
app.listen(app.get("port"), () => {
console.log(app.get("port"), "번 포트에게 대기중");
});