-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca500ac
commit 435bb12
Showing
4 changed files
with
1,108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.