forked from Heize-jiyean/Comma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
145 lines (119 loc) · 3.7 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require('dotenv').config();
const express = require('express');
const mysql = require('mysql2/promise');
const methodOverride = require("method-override");
const app = express();
const path = require('path');
const session = require('express-session');
const port = 3000;
const bodyParser = require('body-parser');
const layouts = require("express-ejs-layouts");
// 지도 API 미들웨어
app.use((req, res, next) => {
res.locals.naverMapClientId = process.env.NAVER_MAP_CLIENT_ID;
next();
});
// DB 연결
exports.connection = async () => {
try {
const db = await mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PW,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
waitForConnections: true,
insecureAuth: true,
connectionLimit: 30,
queueLimit: 10
});
return db; // 연결된 데이터베이스 객체 반환
} catch (error) {
console.error("데이터베이스 연결 오류:", error);
throw error;
}
};
// session 설정
app.use(session({
secret: process.env.SECRET_KEY || 'fallback_secret_key',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
// Firebase SDK 설정
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccountKey.json'); // 서비스 계정 키 파일의 경로
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
// DB connection
require('dotenv').config();
exports.connection = async () => {
try {
const db = await mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PW,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
waitForConnections: true,
insecureAuth: true,
});
return db;
} catch (error) {
console.error("데이터베이스 연결 오류:", error);
throw error;
}
};
// EJS 설정
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// 정적 파일 제공
app.use(express.static(path.join(__dirname, 'public')));
// layouts 사용
app.use(layouts);
// express.json() 미들웨어 추가
app.use(express.json());
// form 데이터 파싱 설정
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// 로깅 미들웨어 추가
app.use((req, res, next) => {
console.log(`Received request: ${req.method} ${req.originalUrl}`);
next();
});
// user 변수 설정을 위한 미들웨어 (통합)
app.use((req, res, next) => {
res.locals.user = req.session.user || null;
next();
});
app.use(methodOverride("_method"));
//layouts 사용
app.use(layouts);
// 페이지 라우팅
app.get('/', (req, res) => {
res.render('main');
});
// 라우트 설정
const authRouter = require('./routers/authRouters');
app.use("/auth", authRouter);
const profileRouter = require('./routers/profileRouter');
app.use('/profile', profileRouter);
const diaryRouter = require('./routers/diaryRouter');
app.use("/diary", diaryRouter);
const guestbookRouter = require('./routers/guestbookRouter');
app.use("/guestbook", guestbookRouter);
const hospitalRouter = require('./routers/hospitalRouters');
app.use("/hospital", hospitalRouter);
// 404 에러 핸들러
app.use((req, res, next) => {
res.status(404).json({ error: 'Not Found', message: 'The requested resource was not found.' });
});
// 오류 처리 미들웨어
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ error: 'Internal Server Error', message: err.message });
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});