-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (55 loc) · 2.06 KB
/
app.js
File metadata and controls
72 lines (55 loc) · 2.06 KB
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
import express from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import { response } from './config/response.js';
import { BaseError } from './config/error.js';
import { status } from './config/response.status.js';
import { userRouter } from './src/routes/userRoute.js';
import mypageRouter from './src/routes/mypageRoute.js';
import recordRouter from './src/routes/recordRoute.js';
import { homeRouter } from './src/routes/homeRoute.js';
import { aiRouter } from './src/routes/aiRoute.js';
import { chatRouter } from './src/routes/chatRoute.js'
// //////
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
// 현재 파일의 디렉터리 경로 얻기
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const uploadsDir = path.join(__dirname, 'uploads');
// 디렉터리가 존재하지 않으면 생성
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir);
}
//////////
dotenv.config();
const app = express();
app.set('port', process.env.PORT || 3000);
app.use(cors());
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// 라우터 설정
app.use('/user', userRouter);
app.use('/mypage', mypageRouter); // 모든 요청을 mypageRouter로 라우팅
app.use('/record', recordRouter);
app.use('/home', homeRouter);
app.use('/predict', aiRouter);
app.use('/chat', chatRouter);
// 에러 핸들링
app.use((req, res, next) => {
const err = new BaseError(status.NOT_FOUND);
next(err);
});
app.use((err, req, res, next) => {
console.log(err);
res.locals.message = err.message;
res.locals.error = process.env.NODE_ENV !== 'production' ? err : {};
// err.data가 정의되지 않았을 때 기본값 설정
const status = err.data ? err.data.status : 500; // 기본값으로 500 (Internal Server Error) 사용
res.status(status).send(response(err.data || { status: 500, message: 'Internal Server Error' }));
});
app.listen(app.get('port'), () => {
console.log(`Example app listening on port ${app.get('port')}`);
});