@pirxpilot/router
v2.0.0
Published
Simple middleware-style router
Readme
@pirxpilot/router
This is a simplified fork of router with some dependencies removed.
Router Options
The router accepts an options object that can be used to customize its behavior:
const router = Router({
caseSensitive: false, // Enable case-sensitive routing
mergeParams: false, // Preserve req.params values from parent router
strict: false // Enable strict routing
})Available options:
caseSensitive- Enable case-sensitive routing. By default routes are case-insensitive (e.g., "/foo" and "/FOO" are treated the same).mergeParams- Preserve thereq.paramsvalues from the parent router. If the parent and the child have conflicting param names, the child's value take precedence.strict- Enable strict routing. By default "/foo" and "/foo/" are treated the same by the router.
Pattern Matching with URLPattern API
This router uses the URLPattern API for path matching.
Basic Pattern Examples
- Named parameters:
router.get('/user/:id', (req, res) => { ... }) // matches /user/123, /user/abc- Optional parameters:
router.get('/user/:name?', (req, res) => { ... }) // matches /user, /user/john- Wildcard/Greedy matching:
router.get('/files/:path*', (req, res) => { ... }) // matches /files, /files/docs, /files/docs/intro.pdfAdvanced Pattern Examples
- Custom parameter patterns:
router.get('/user/:id(\\d+)', (req, res) => { ... }) // matches /user/123 but not /user/abc- Multiple parameters:
router.get('/api/:version/:resource', (req, res) => { ... }) // matches /api/v1/users- Group matching:
router.get('/:category(books|movies)/:id', (req, res) => { ... }) // matches /books/123 or /movies/456