File tree Expand file tree Collapse file tree 3 files changed +30
-2
lines changed Expand file tree Collapse file tree 3 files changed +30
-2
lines changed Original file line number Diff line number Diff line change
1
+ const rateLimit = require ( 'express-rate-limit' )
2
+
3
+ const rateLimiter = rateLimit ( {
4
+ windowMs : 5 * 60 * 1000 , // 5 mins in milliseconds
5
+ max : 4 ,
6
+ message : { error429 : 'Sorry, you can make only 4 requests every 5 minutes!' } ,
7
+ headers : true ,
8
+ } )
9
+
10
+ module . exports = rateLimiter
Original file line number Diff line number Diff line change 13
13
"cors" : " ^2.8.5" ,
14
14
"dotenv" : " ^10.0.0" ,
15
15
"express" : " ^4.17.1" ,
16
+ "express-rate-limit" : " ^5.5.1" ,
16
17
"mongoose" : " ^6.0.5" ,
17
18
"morgan" : " ^1.10.0" ,
18
19
"uuid" : " ^8.3.2"
Original file line number Diff line number Diff line change @@ -4,17 +4,34 @@ const cors = require('cors')
4
4
const morgan = require ( 'morgan' )
5
5
require ( 'dotenv' ) . config ( )
6
6
7
+ const rateLimiter = require ( './middlewares/rateLimiter' )
7
8
const api = require ( './routes/api' )
8
9
const connectDB = require ( './connectDB' )
9
10
10
11
const app = express ( )
11
12
const PORT = process . env . PORT || 5000
12
13
13
- app . use ( cors ( ) )
14
- app . use ( morgan ( 'dev' ) )
15
14
app . use ( express . json ( ) )
16
15
app . use ( express . urlencoded ( { extended : false } ) )
17
16
17
+ // Middlewares
18
+
19
+ // Logger
20
+ app . use ( morgan ( 'dev' ) )
21
+
22
+ // CORS
23
+ const corsOptions = {
24
+ credentials : true ,
25
+ origin : [
26
+ / w a l l e t .z o h a i b .i n $ / ,
27
+ / l o c a l h o s t / ,
28
+ ] ,
29
+ }
30
+ app . use ( cors ( corsOptions ) )
31
+
32
+ // Rate limiter
33
+ app . use ( rateLimiter )
34
+
18
35
// Use Routes
19
36
if ( process . env . NODE_ENV === 'development' ) {
20
37
// Simulate delay while accessing db on local
You can’t perform that action at this time.
0 commit comments