वेबसाइटों को डॉस और डीडीओएस हमलों से बचाने के लिए दर-सीमित करना दिन-प्रतिदिन महत्वपूर्ण होता जा रहा है। दर-सीमित प्रणाली को किसी भी प्रकार के नकली अनुरोधों या अन्य क्रूर बल के हमलों से रोकता है। दर सीमित करने से आईपी द्वारा अनुरोध किए जाने की संख्या सीमित हो जाती है। एक्सप्रेसरेट-सीमा एक उपयोगकर्ता से अनुरोधों की संख्या को सीमित करने के लिए npm पैकेज है।
दर-सीमा मॉड्यूल स्थापित करना
अपने एप्लिकेशन में एक्सप्रेस रेट-लिमिटिंग मॉड्यूल स्थापित करने के लिए नीचे दिया गया कमांड चलाएँ।
npm install --save express-rate-limit
उदाहरण
नाम के साथ एक फाइल बनाएं – rateLimit.js और नीचे दिए गए कोड स्निपेट को कॉपी करें। फ़ाइल बनाने के बाद, इस कोड को चलाने के लिए निम्न कमांड का उपयोग करें जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है -
node rateLimit.js
rateLimit.js
// Importing the express dependency
const express = require("express");
// Importing the express-rate-limit dependency
const rateLimit = require("express-rate-limit");
// Storing the express function in variable application
const applicaion = express();
// Calling the ratelimiter function with its options
// max: Contains the maximum number of requests
// windowsMs: Contains the time in milliseconds to receive max requests
// message: message to be shown to the user on rate-limit
const limiter = rateLimit({
max: 5,
windowMs: 60 * 60 * 1000,
message: "Too many request from this IP"
});
// Adding the rate-limit function to the express middleware so
// that each requests passes through this limit before executing
applicaion.use(limiter);
// GET route for handling the user requests
applicaion.get("/", (req, res) => {
res.status(200).json({
status: "SUCCESS",
message: "Welcome to TutorialsPoint !"
});
});
// Server Setup
const port = 8000;
applicaion.listen(port, () => {
console.log(`app is running on port ${port}`);
}); आउटपुट
C:\home\node>> node rateLimit.js
नोड एप्लिकेशन चलाने के बाद, अपने ब्राउज़र पर जाएं और लोकलहोस्ट को हिट करें:8000
जैसा कि नीचे दिखाया गया है, आपको ऐसा ही एक पेज दिखाई देगा।

एक ही URL को 5 से अधिक बार हिट या रीफ्रेश करने का प्रयास करें और आपको निम्न त्रुटि प्राप्त होगी।
