express-match-request
raw JSON → 0.1.3 verified Sat Apr 25 auth: no javascript maintenance
Express middleware for matching incoming requests (method + URL) to a pattern, commonly used to conditionally apply further middleware or route handlers. Latest version 0.1.3, released sporadically. Minimal utility with no dependencies; relies on Express's req.method and req.originalUrl for pattern matching. Not actively maintained, with only a few releases. Simpler than full-featured route-matching libraries like express-path-match or find-my-way, but may not support advanced patterns or wildcards.
Common errors
error TypeError: matchRequest is not a function ↓
cause Using named import instead of default import in ESM (e.g., import { matchRequest } from 'express-match-request')
fix
Use default import: import matchRequest from 'express-match-request'
error Cannot find module 'express-match-request' ↓
cause Package not installed or incorrectly installed
fix
Run: npm install express-match-request
error matchRequest(...) is not a valid middleware ↓
cause matchRequest returns a function that must be used as middleware; if called without arguments it returns undefined
fix
Ensure you pass method and pattern: app.use(matchRequest('GET', '/path'), handler)
Warnings
gotcha Pattern matching is simple equality, not RegExp or path-to-regexp. Complex patterns (e.g., '/users/:id') will not work as expected. ↓
fix Use a library like express-path-match for parameterized routes.
deprecated Library has not been updated in years; no TypeScript definitions or modern ESM support beyond basic CommonJS. ↓
fix Consider alternatives like find-my-way or express-path-match.
gotcha If method is '*', it matches all HTTP methods (GET, POST, etc.) but does not distinguish. In Express, middleware order and next() semantics apply. ↓
fix Ensure subsequent middleware handles non-matched methods correctly.
Install
npm install express-match-request yarn add express-match-request pnpm add express-match-request Imports
- default wrong
const { matchRequest } = require('express-match-request')correctimport matchRequest from 'express-match-request' - matchRequest wrong
import { matchRequest } from 'express-match-request'correctconst matchRequest = require('express-match-request') - Pattern
import matchRequest from 'express-match-request'
Quickstart
import express from 'express';
import matchRequest from 'express-match-request';
const app = express();
// Match requests to GET /api/users
app.use(matchRequest('GET', '/api/users'), (req, res) => {
res.json({ message: 'Users endpoint' });
});
// Match all methods to /public
app.use(matchRequest('*', '/public'), express.static('public'));
app.listen(3000, () => console.log('Server running on port 3000'));