forked from woojong94/yhcoffee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (61 loc) · 1.75 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
const express = require("express");
const nunjucks = require("nunjucks");
const morgan = require("morgan");
const dotenv = require("dotenv");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const path = require("path");
const logger = require("./lib/logger");
const bootStrap = require("./boot");
/* 라우터 */
const memberRouter = require("./routes/member");
const mainRouter = require("./routes")
const app = express();
dotenv.config();
app.set("PORT", process.env.PORT || 3004);
app.set("view engine", "html");
nunjucks.configure(path.join(__dirname, "views"), {
express : app,
watch : true,
});
app.use(morgan("dev"));
app.use(express.static(path.join(__dirname, "public")));
app.use(express.json());
app.use(express.urlencoded({ extended : false }));
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(session({
resave : false,
saveUninitialized : true,
secret : process.env.COOKIE_SECRET,
cookie : {
httpOnly : true,
},
name : "yhsessid",
}));
app.use(bootStrap);
/* 라우터 등록 */
app.use("/", mainRouter);
app.use("/member", memberRouter);
app.use((req, res, next) => {
const err = new Error(`${req.url}은 존재하지 않는 페이지 입니다.` );
err.status = 404;
next(err);
});
/* 오류 처리 라우터 */
app.use((err, req, res, next) => {
const data = {
message : err.message,
status : err.status || 500,
stack : err.stack,
};
/* 로그 기록 */
logger(`[${data.status}]${data.message}` , "error");
logger(data.stack, "error");
if (process.env.NODE_ENV === "production") {
delete data.stack;
}
return res.status(data.status).render("error", data);
});
app.listen(app.get("PORT"), () => {
console.log(app.get("PORT"), "번 포트에서 서버 대기중!!!!!");
});