express-defender
v1.0.1
Published
Express middleware to enforce domain and country-based access, blocking direct IP requests and unwanted bots.
Downloads
11
Maintainers
Readme
About
Express middleware to enforce domain and country-based access, blocking direct IP requests and unwanted bots.
⭐ Like Express Defender? Star it on GitHub to support the project!
Installation
npm i express-defenderBasic Usage
const express = require('express');
const expressDefender = require('express-defender');
const app = express();
app.use(expressDefender({
allowedDomains: ['example.com'],
allowedCountries: ['BR', 'US']
}));
app.get('/', (req, res) => {
res.json(req.expressDefender);
});
app.listen(3000, () => console.log('Server running on http://localhost:3000'));| Option | Type | Default | Description |
| ------------------ | ---------- | -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| allowedDomains | string[] | ['*'] | List of allowed domains. Accepts * to allow all. E.g., ['example.com'] also allows www.example.com and api.example.com. |
| allowedCountries | string[] | ['*'] | List of allowed countries (ISO Alpha-2). Use * to allow all. |
| allowedBots | RegExp[] | Googlebot, Bingbot, Slurp, DuckDuckBot | List of User-Agents of bots authorized to ignore country/domain restrictions. |
| log | boolean | true | Enables or disables logging in the console. |
Examples
• Allow all domains and countries
app.use(expressDefender({
allowedDomains: ['*'],
allowedCountries: ['*']
}));• Allow only the US and Canada
app.use(expressDefender({
allowedCountries: ['US', 'CA']
}));• Allow only direct requests from mysite.com and Googlebot
app.use(expressDefender({
allowedDomains: ['mysite.com'],
allowedBots: [/Googlebot/]
}));Extra
• Output of req.expressDefender
{
"ip": "192.0.2.25",
"country": "BR",
"region": "SP",
"city": "SP",
"method": "GET",
"url": "/home",
"fromDomain": true,
"isBot": false
}• Logs when
Bot Acess
Localhost Acess
Sucessful Acess
Blocked Acess by Country
Blocked Acess by direct IP traffic
Blocked Acess by Country and direct IP traffic
respectively.
Contributing - bug fixes
Contributions are welcome! Please feel free to open an issue or submit a pull request, for bug fixes or new features.
- Fork the repository
- Create a new branch
git checkout -b <new-feature-name> - Make the changes
- Commit the changes
git commit -am "Add new feature" - Push the changes
git push origin <new-feature-name> - Create a pull request on GitHub
