-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
117 lines (98 loc) · 3.12 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
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
const path = require('path');
const express = require('express');
const cors = require('cors');
const xss = require('xss-clean');
const cookieParser = require('cookie-parser');
const rateLimit = require('express-rate-limit');
const morgan = require('morgan');
const AppError = require('./utils/appError');
const GlobalErrorHandler = require('./controllers/errorController');
const { createServer } = require('http');
const usersRouter = require('./routes/userRoutes');
const postRoutes = require('./routes/postRoutes');
const friendsRoutes = require('./routes/friendsRoutes');
const messagesRoutes = require('./routes/messagesRoutes');
const chatRoutes = require('./routes/chatRoutes');
const app = express();
const whitelist = [
'http://127.0.0.1:3000',
'http://192.168.1.2:3000',
'http://192.168.1.6:3000',
'http://192.168.1.2:8000',
'http://localhost:3000',
'https://backbook.vercel.app',
'https://backbook-api.cyclic.app',
];
const corsOptions = {
credentials: true,
origin: function (origin, callback) {
if (!origin) {
//for bypassing postman req with no origin
return callback(null, true);
}
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
};
// const whitelist = ['http://127.0.0.1:3000', 'http://192.168.1.2:3000'];
// const corsOptions = {
// origin: function (origin, callback) {
// if (!origin) {
// // for bypassing postman req with no origin 0
// return callback(null, true);
// }
// if (whitelist.indexOf(origin) !== -1) {
// callback(null, true);
// } else {
// callback(new Error('Not allowed by CORS'));
// }
// },
// };
app.use(cors(corsOptions));
const limiter = rateLimit({
windowMs: 60 * 60 * 1000 * 24,
max: 20,
handler: (request, response, next, options) =>
response.status(options.statusCode).json({
status: 'fail ',
message:
'You can only post 15 posts per day and you have reached the limit. You can post again tomorrow, have fun 😉',
}),
});
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
app.use('/api/v1/posts/createPost', limiter);
app.use(express.json({ limit: '5000kb' }));
app.use(express.urlencoded({ extended: true, limit: '5000kb' }));
app.use(cookieParser());
app.use(xss());
app.set('view engine', 'pug');
// app.use(express.static('public'));
// app.use(express.static(path.join(__dirname, 'build')));
app.use('/api/v1/users', usersRouter);
app.use('/api/v1/posts', postRoutes);
app.use('/api/v1/friends', friendsRoutes);
app.use('/api/v1/chats', chatRoutes);
app.use('/api/v1/messages', messagesRoutes);
// app.use((req, res, next) => {
// res.sendFile(path.join(__dirname, 'build', 'index.html'));
// });
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl}`, 404));
});
app.use(GlobalErrorHandler);
const httpServer = createServer(app);
const sio = require('./utils/socket');
sio.init(httpServer, {
pingTimeout: 60000,
pingInterval: 60000,
cors: {
origin: whitelist,
},
});
exports.whitelist = whitelist;
exports.httpServer = httpServer;