-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path01-ratelimitter.js
More file actions
27 lines (21 loc) · 893 Bytes
/
01-ratelimitter.js
File metadata and controls
27 lines (21 loc) · 893 Bytes
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
// You have to create a middleware for rate limiting a users request based on their username passed in the header
const express = require('express');
const app = express();
// Your task is to create a global middleware (app.use) which will
// rate limit the requests from a user to only 5 request per second
// If a user sends more than 5 requests in a single second, the server
// should block them with a 404.
// User will be sending in their user id in the header as 'user-id'
// You have been given a numberOfRequestsForUser object to start off with which
// clears every one second
let numberOfRequestsForUser = {};
setInterval(() => {
numberOfRequestsForUser = {};
}, 1000)
app.get('/user', function(req, res) {
res.status(200).json({ name: 'john' });
});
app.post('/user', function(req, res) {
res.status(200).json({ msg: 'created dummy user' });
});
module.exports = app;