-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.ts
More file actions
90 lines (83 loc) · 2.49 KB
/
server.ts
File metadata and controls
90 lines (83 loc) · 2.49 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import express from 'express';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import dotenv from 'dotenv';
import helmet from 'helmet'; //악성 스크립트 보호
import HTTPS from 'https';
import fs from 'fs';
import hpp from 'hpp'
import indexRouter from './src/api/routes/index';
import createData from './src/database/data';
import {errorLogger, errorHandler} from './src/middlewares/errorhandler';
import { sequelize } from './src/database/models/sequlize';
dotenv.config();
const app = express();
const prod: boolean = process.env.NODE_ENV === 'production';
app.set('port', prod ? process.env.PORT : 3000);
app.use(helmet())
app.use(hpp())
const whitelist = [
"http://localhost:3000",
process.env.Client_1,
undefined
]
const corsOptions = {
origin: function (origin:any, callback:any) {
if (whitelist.indexOf(origin) !== -1) {
// 만일 whitelist 배열에 origin인자가 있을 경우
callback(null, true); // cors 허용
} else {
callback(new Error("Not Allowed Origin!")); // cors 비허용
}
},
methods: "GET,POST,PUT,DELETE,PATCH",
credentials: true,
};
app.use(cors(corsOptions)) //옵션 추가한 cors 미들웨어 추가
app.use(express.json());
app.use(cookieParser());
app.use('/', indexRouter);
//에러발생시 logger로 넘어옴
app.use(errorLogger);
//에러발생시 Handler로 이동
app.use(errorHandler);
if (prod) {
try {
const options = {
ca: fs.readFileSync(`${process.env.CA}`),
key: fs.readFileSync(`${process.env.KEY}`),
cert: fs.readFileSync(`${process.env.CERT}`),
};
HTTPS.createServer(options, app).listen(app.get('port'), async () => {
console.log('https 서버가 실행되었습니다. 포트 :: ' + app.get('port'));
createData();
await sequelize
.authenticate()
.then(async () => {
console.log('DB 연결완료');
})
.catch((err) => {
console.log(err);
console.log('DB 연결실패');
});
});
} catch (err) {
console.log('HTTPS 서버가 실행되지 않습니다.');
console.log(err);
}
} else {
app.listen(app.get('port'), async () => {
console.log('HTTPS 서버가 실행되지 않습니다.');
console.log(`${app.get('port')}로 실행중`);
createData();
await sequelize
.authenticate()
.then(async () => {
console.log('DB 연결완료');
})
.catch((err) => {
console.log(err);
console.log('DB 연결실패');
});
});
}