-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (73 loc) · 2.41 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
const express = require('express');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const cors = require('cors');
const AppError = require('./utils/AppError');
const path = require('path');
const bodyParser = require('body-parser');
const bookingController = require('./controllers/bookingController');
const app = express();
// Trust the proxy to get the correct client IP
// app.set('trust proxy', 1);
// CORS configuration to allow all origins and credentials
const corsOptions = {
origin: (origin, callback) => {
callback(null, true); // Allow all origins
},
methods: 'GET,POST,PUT,DELETE,PATCH,OPTIONS',
allowedHeaders: 'Content-Type,Authorization',
credentials: true,
preflightContinue: false,
optionsSuccessStatus: 204,
};
app.use(cors(corsOptions));
app.options('*', cors(corsOptions)); // Enable pre-flight across-the-board
// webhook url
app.post(
'/webhook-checkout',
bodyParser.raw({ type: 'application/json' }),
bookingController.webhookCheckout
);
app.use(helmet());
app.use(express.json({ limit: '10kb' }));
app.use(mongoSanitize());
app.use(xss());
app.use(express.static(path.join(__dirname, 'public')));
// Rate limiting
const limiter = rateLimit({
windowMs: 60 * 60 * 1000,
limit: 100,
message: 'Too many requests from this IP, please try again after an hour',
});
// app.use('/api', limiter);
// Static files
app.use(express.static(`${__dirname}/public`));
// Routes
const CabinRouter = require('./routes/cabinRoute');
const BookingRouter = require('./routes/bookingRoute');
const GuestRouter = require('./routes/guestRoute');
const UserRouter = require('./routes/userRoute');
const SettingsRouter = require('./routes/settingsRoute');
// app.use((req, res, next) => {
// console.log('request url', req.originalUrl);
// next();
// });
app.use('/cabins', CabinRouter);
app.use('/bookings', BookingRouter);
app.use('/guests', GuestRouter);
app.use('/users', UserRouter);
app.use('/settings', SettingsRouter);
// Handling unmatched routes
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
// Global error handler
app.use((err, req, res, next) => {
res.status(err.statusCode || 500).json({
status: err.status || 'error',
message: err.message || 'An unknown error occurred',
});
});
module.exports = app;