-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
78 lines (64 loc) · 2.08 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
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import helmet from 'helmet'
import { rateLimit } from 'express-rate-limit'
import ExpressMongoSanitize from 'express-mongo-sanitize'
import dotenv from 'dotenv'
dotenv.config({ path: './config/config.env' })
import './config/db.js'
import locationsRoutes from './api/locations/location.api.routes.js'
import departmentsRoutes from './api/departments/departments.api.routes.js'
import regionsRoutes from './api/regions/regions.api.routes.js'
import newRegionsRoutes from './api/new-regions/new_regions.api.routes.js'
const app = express();
app.use(cors({
'credentials': true,
'origin': '*',
"Access-Control-Allow-Origin": true,
'allowedHeaders': ['Content-Type', 'Authorization', 'application'],
'methods': 'GET',
'preflightContinue': false,
}))
app.use(helmet())
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 150,
standardHeaders: true,
legacyHeaders: false,
message: 'Too Many Request from this IP, your IP has been blocked. Please try again later.'
})
app.use(limiter)
app.use(express.json({ limit: '15kb' }))
app.use(bodyParser.urlencoded({
extended: false,
limit: '15kb'
}))
app.use(bodyParser.json({ limit: '15kb' }))
app.use(ExpressMongoSanitize({
allowDots: true,
}));
app.use('/search/locations', locationsRoutes)
app.use('/search/departments', departmentsRoutes)
app.use('/search/regions', regionsRoutes)
app.use('/search/new-regions', newRegionsRoutes)
app.get("*", async (req, res) => {
return res.status(400).json({ error: `Cette route n'existe pas.` })
})
if (process.env.NODE_ENV !== 'production') {
process.once('uncaughtException', err => {
console.error(err.stack || err)
setTimeout(() => process.exit(1), 100)
})
}
process.on('unhandledRejection', err => {
console.log('Erreur :');
console.log(err.name, err.message);
server.close(() => {
process.exit(1);
});
});
const PORT = process.env.PORT || 5000
app.listen(PORT, () => {
console.log(`Serveur démarré : http://localhost:${PORT}`)
})