-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·61 lines (53 loc) · 1.62 KB
/
server.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
const express = require('express');
const router = require('./Routes/router');
const path = require('path');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const timeout = require('connect-timeout');
const RateLimit = require('express-rate-limit');
const helmet = require('helmet');
require('dotenv').config();
const app = express();
const port = 5050;
app.use(express.json());
const cookieSecret = process.env.COOKIE_SECRET_KEY;
app.use(cookieParser(cookieSecret));
app.use(
cors({
origin: process.env.CLIENT_URL,
methods: ['GET', 'POST', 'PATCH', 'DELETE'],
credentials: true,
})
);
app.use(timeout('5s'));
//api 요청 제한 미들웨어
exports.apiLimiter = RateLimit({
windowMs: 60 * 10000, //1분
max: 50,
handler(req, res) {
res.status(this.statusCode).json({
code: this.statusCode, //RateLimit의 반환객체는 429code를 default로 반환하게 되어있음
message: '1분에 5번만 요청 할 수 있습니다.',
});
},
});
// CSP 설정
const cspOptions = {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'"],
styleSrc: ["'self'"],
imgSrc: ["'self'"],
},
};
app.use(helmet());
app.use(helmet.contentSecurityPolicy(cspOptions));
app.use(helmet.crossOriginResourcePolicy({ policy: 'cross-origin' }));
app.use('/api', this.apiLimiter, router);
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.use('/resource', express.static(path.join(__dirname, 'resource')));
app.listen(port, () => {
console.log(`Sever is running on http://localhost:${port}`);
});