-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
103 lines (86 loc) · 2.53 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
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
/* eslint-disable import/no-extraneous-dependencies */
const path = require('path');
// eslint-disable-next-line import/no-extraneous-dependencies
const hpp = require('hpp');
const cors = require('cors');
const compression = require('compression');
const ratelimit = require('express-rate-limit');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const cookieParser = require('cookie-parser');
const express = require('express');
const dotenv = require('dotenv');
const morgan = require('morgan');
dotenv.config({ path: 'config.env' });
const ApiError = require('./utils/apiError');
const globalError = require('./middlewares/errorMiddleware');
const dbConnection = require('./config/database');
// Routes
const mountRoutes = require('./routes');
const { webhookCheckout } = require('./controllers/orderController');
// Connect with db
dbConnection();
// express app
const app = express();
// Enable CROS
app.use(cors());
app.options('*', cors());
// compress all responses
app.use(compression());
// checkout webhook
app.post(
'/webhook-checkout',
express.raw({ type: 'application/json' }),
webhookCheckout
);
// Middlewares
app.use(express.json({ limit: '20kb' }));
app.use(express.static(path.join(__dirname, 'uploads')));
// Cookie Parser
app.use(cookieParser());
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
console.log(`mode ${process.env.NODE_ENV}`);
}
// To apply data sanitization
app.use(mongoSanitize());
app.use(xss());
const limiter = ratelimit({
windowMs: 60 * 60 * 1000, //1 hour
max: 100,
message:
'Too many accounts created from this IP, please try again after an hour',
});
// Apply the rate limitimg middleware to all requests
app.use('/api/v1/auth', limiter);
// Middleware to protect against HTTP Parameter Pollution attacks
app.use(
hpp({
whitelist: [
'price',
'sold',
'quantity',
'ratingsAverage',
'ratingsQuantity',
],
})
);
// Mount Routes
mountRoutes(app);
app.all('*', (req, res, next) => {
next(new ApiError(`Can't find this route: ${req.originalUrl}`, 404));
});
// Global error handling middleware For express
app.use(globalError);
const PORT = process.env.PORT || 8000;
const server = app.listen(PORT, () => {
console.log(`App running on port ${PORT}`);
});
// Handle rejection outside express
process.on('unhandledRejection', (err) => {
console.error(`UnhandledRegjection Errors: ${err.name}| ${err.message}`);
server.close(() => {
console.error('Shutting down....');
process.exit(1);
});
});