rate-limiter.js
import rateLimiter from "express-rate-limit";
export default rateLimiter({
windowMs: 10 * 1000,
max: 10,
message: "요청이 너무 많습니다.",
handler: (req, res, next, options) => {
console.log("options.statusCode -", options.statusCode);
console.log("options.message -", options.message);
res.status(options.statusCode).send(options.message);
},
});
app.js
import express from "express";
import helmet from "helmet";
import morgan from "morgan";
import cors from "cors";
import tweetController from "./tweet/tweet.controller.js";
import authController from "./auth/auth.controller.js";
import { config } from "../config.js";
import sequelize from "../database.js";
import csrf from "./middleware/csrf.js";
import rateLimiter from "./middleware/rate-limiter.js";
const app = express();
app.use(csrf);
app.use(express.json());
app.use(morgan("dev"));
app.use(helmet());
app.use(cors());
app.use(rateLimiter);
app.use("/tweet", tweetController);
app.use("/auth", authController);
app.use((req, res, next) => {
res.sendStatus(404);
});
app.use((err, req, res, next) => {
res.sendStatus(500);
});
sequelize.sync().then(() => {
app.listen(config.port, () => {
console.log("Server On...");
});
});
- 10번이 limit인데 11번 요청했을 경우, 마지막 11번째 요청은
429: Too Many Request