API 속도 제한 설정

Express용 고정 윈도 속도 제한 미들웨어 코드를 생성합니다

const hits = new Map() // ip -> { count, resetAt }

export function rateLimit(req, res, next) {
  const now = Date.now()
  const h = hits.get(req.ip)
  if (!h || h.resetAt <= now) {
    hits.set(req.ip, { count: 1, resetAt: now + 60000 })
    return next()
  }
  if (++h.count > 100) {
    res.set('Retry-After', Math.ceil((h.resetAt - now) / 1000))
    return res.status(429).json({ error: 'Too Many Requests' })
  }
  next()
}

속도 제한 설정 도구 소개

API 속도 제한과 쓰로틀링을 설정합니다 별도 설치나 회원가입 없이 브라우저에서 바로 사용할 수 있습니다.

관련 키워드: 속도제한, 쓰로틀링, API, 보안

함께 쓰기 좋은 개발 도구 도구