Socket
Book a DemoInstallSign in
Socket

express-defender

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-defender

Express middleware to enforce domain and country-based access, blocking direct IP requests and unwanted bots.

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
72
Maintainers
1
Weekly downloads
 
Created
Source

express-defender


NPM version NPM downloads GitHub Version GitHub Version

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-defender

Basic 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'));
OptionTypeDefaultDescription
allowedDomainsstring[]['*']List of allowed domains. Accepts * to allow all. E.g., ['example.com'] also allows www.example.com and api.example.com.
allowedCountriesstring[]['*']List of allowed countries (ISO Alpha-2). Use * to allow all.
allowedBotsRegExp[]Googlebot, Bingbot, Slurp, DuckDuckBotList of User-Agents of bots authorized to ignore country/domain restrictions.
logbooleantrueEnables 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.

express-defender example

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

Keywords

express

FAQs

Package last updated on 03 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.