Skip to content

Commit 3faaa57

Browse files
committed
✨ slow down requests
1 parent 07b12ca commit 3faaa57

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const helmet = require('helmet');
66
const yup = require('yup');
77
const monk = require('monk');
88
const rateLimit = require('express-rate-limit');
9+
const slowDown = require('express-slow-down');
910
const { nanoid } = require('nanoid');
1011

1112
require('dotenv').config();
@@ -43,8 +44,12 @@ const schema = yup.object().shape({
4344
url: yup.string().trim().url().required(),
4445
});
4546

46-
app.post('/url', rateLimit({
47-
windowMs: 30 * 1000, // 30 seconds
47+
app.post('/url', slowDown({
48+
windowMs: 30 * 1000,
49+
delayAfter: 1,
50+
delayMs: 500,
51+
}), rateLimit({
52+
windowMs: 30 * 1000,
4853
max: 1,
4954
}), async (req, res, next) => {
5055
let { slug, url } = req.body;

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dotenv": "^8.2.0",
1616
"express": "^4.17.1",
1717
"express-rate-limit": "^5.1.3",
18+
"express-slow-down": "^1.3.1",
1819
"helmet": "^3.22.0",
1920
"monk": "^7.3.0",
2021
"morgan": "^1.10.0",

0 commit comments

Comments
 (0)