Skip to content

Commit

Permalink
add nodejs-rate-limiter code
Browse files Browse the repository at this point in the history
  • Loading branch information
ajibade3210 committed Sep 5, 2023
1 parent ca500ac commit 435bb12
Show file tree
Hide file tree
Showing 4 changed files with 1,108 additions and 0 deletions.
27 changes: 27 additions & 0 deletions nodejs/nodejs-rate-limiter/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const express = require("express");
const rateLimitMiddleware = require("./middlewares/ratelimit");
const app = express();

app.use(rateLimitMiddleware);

// A simple API route
app.get("/api/blog", (req, res) => {
res.send({
success: true,
message: "Welcome to our Blog API Rate Limiter Project 🎉",
});
});

app.get("/api/blog/post", (req, res) => {
res.send({
success: true,
author: "Mike Abdul",
title: "Creating NodeJs Rate Limiter",
post: "...",
});
});

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
11 changes: 11 additions & 0 deletions nodejs/nodejs-rate-limiter/middlewares/ratelimit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const setRateLimit = require("express-rate-limit");

// Rate limit middleware
const rateLimitMiddleware = setRateLimit({
windowMs: 60 * 1000,
max: 5,
message: "You have exceeded your 5 requests per minute limit.",
headers: true,
});

module.exports = rateLimitMiddleware;
Loading

0 comments on commit 435bb12

Please sign in to comment.