express-ip-filter-middleware is an Express.js middleware designed for robust access control, enabling developers to filter incoming requests based on IP addresses or CIDR blocks. It leverages Node.js's built-in `net.BlockList` for efficient management of allowed and denied IP ranges. The current stable version is 2.0.2, with recent releases primarily focusing on dependency updates, indicating an active maintenance cadence. Key differentiators include its explicit `whitelist` and `blacklist` modes, which mimic Apache's `mod_access` behavior, offering precise control over how allow and deny rules interact. It also provides an `ipOverride` option for custom IP address extraction, making it flexible for environments behind proxies or load balancers.
npm install express-ip-filter-middlewareVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes an Express app, configures an IP filter in 'whitelist' mode using `node:net.BlockList`, and applies it globally to demonstrate basic access control.
Always explicitly define the `mode` option when configuring the `ipFilterMiddleware`.
Ensure that your `allow` list contains the necessary IP addresses or CIDR blocks when operating in 'whitelist' mode, or configure `deny` with explicit blocks if you intend to allow everything else.
Implement robust validation within your `ipOverride` function to ensure it always returns a valid IPv4/IPv6 string or `undefined`. Consider using a `try-catch` block around the middleware in your Express application or a global error handler to gracefully manage such errors.
Review your existing `express-ip-filter-middleware` configurations against the v2 documentation and thoroughly test all IP-filtering scenarios, particularly edge cases involving empty lists, invalid IPs, or proxy setups, when upgrading.
For ESM, use `import { ipFilterMiddleware } from 'express-ip-filter-middleware';`. For CJS, use `const { ipFilterMiddleware } = require('express-ip-filter-middleware');`.Add a `mode` property to your options object, e.g., `{ mode: 'whitelist', allow, deny }`.Ensure that the `allow` `BlockList` instance is populated with all desired IP addresses or networks when using `mode: 'whitelist'`.