속도 제한은 DOS 및 DDOS 공격으로부터 웹사이트를 방지하기 위해 나날이 중요해지고 있습니다. 속도 제한은 시스템이 모든 유형의 가짜 요청 또는 기타 무차별 대입 공격을 방지합니다. 속도 제한은 IP가 요청할 수 있는 횟수를 제한합니다. expressrate-limit는 사용자의 요청 수를 제한하는 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
노드 애플리케이션을 실행한 후 브라우저로 이동하여 localhost:8000
을 누르십시오.아래와 같은 유사한 페이지가 표시됩니다.

동일한 URL을 5번 이상 누르거나 새로고침하면 아래와 같은 오류가 발생합니다.
