-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
97 lines (78 loc) · 1.88 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
90
91
92
93
94
95
96
97
const express = require("express");
const nunjucks = require("nunjucks");
const logger = require("morgan");
const bodyParser = require("body-parser");
// db 관련
const db = require("./models");
class App {
constructor() {
this.app = express();
// db 접속
this.dbConnection();
// 뷰엔진 셋팅
this.setViewEngine();
// 미들웨어 셋팅
this.setMiddleWare();
// 정적 디렉토리 추가
this.setStatic();
// 로컬 변수
this.setLocals();
// 라우팅
this.getRouting();
// 404 페이지를 찾을수가 없음
this.status404();
// 에러처리
this.errorHandler();
}
dbConnection() {
// DB authentication
db.sequelize
.authenticate()
.then(() => {
console.log("Connection has been established successfully.");
})
.then(() => {
console.log("DB Sync complete.");
})
.catch((err) => {
console.error("Unable to connect to the database:", err);
});
}
setMiddleWare() {
// 미들웨어 셋팅
this.app.use(logger("dev"));
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
}
setViewEngine() {
nunjucks.configure("template", {
autoescape: true,
express: this.app,
});
}
setStatic() {
this.app.use("/uploads", express.static("uploads"));
}
setLocals() {
// 템플릿 변수
this.app.use((req, res, next) => {
this.app.locals.isLogin = true;
this.app.locals.req_path = req.path;
next();
});
}
getRouting() {
this.app.use(require("./controllers"));
}
status404() {
this.app.use((req, res, _) => {
res.status(404).render("common/404.html");
});
}
errorHandler() {
this.app.use((err, req, res, _) => {
res.status(500).render("common/500.html");
});
}
}
module.exports = new App().app;