# koa-path-match

> koa route middleware

Latest version **5.0.0** (published 2025-03-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-path-match
pnpm add koa-path-match
yarn add koa-path-match
bun add koa-path-match
```

## Health

**Score 35/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2025-03-20 |
| First published | 2014-07-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 6.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 37 |
| Author | Jonathan Ong |
| Maintainers | coderhaoxin, jongleberry |
| Keywords | koa, route, router |

## Links

- npm: https://www.npmjs.com/package/koa-path-match
- Repository: https://github.com/koajs/path-match
- Homepage: https://github.com/koajs/path-match#readme
- Issues: https://github.com/koajs/path-match/issues
- npm.io page: https://npm.io/package/koa-path-match

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^8.2.0

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 5.0.0 (latest) — 2025-03-20
- 2.0.0 (next) — 2016-02-25
- 4.0.0 — 2024-08-31
- 3.0.0 — 2020-05-02
- 1.2.1 — 2016-10-17
- 1.2.0 — 2016-02-28
- 1.1.1 — 2016-02-17
- 1.1.0 — 2015-12-24
- 1.0.1 — 2014-07-23

## README

# Koa Path Match

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/koajs/path-match/workflows/Node.js%20CI/badge.svg?branch=master)](https://github.com/koajs/path-match/actions?query=workflow%3A%22Node.js+CI%22)
[![Test coverage][codecov-image]][codecov-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]

A simple routing wrapper around [path-match](https://github.com/expressjs/path-match).
Similar to [koa-route](https://github.com/koajs/route), except it optionally handles methods better.
All of these routers use [path-to-regexp](https://github.com/component/path-to-regexp)
underneath, which is what Express uses as well.

```js
const route = require('koa-path-match')({/* options passed to path-to-regexp */})

app.use(route('/:id(\\d+)', (ctx, next) => {
  const id = ctx.params.id

  // do stuff
  switch (ctx.request.method) {

  }
}))
```

Or you can create middleware per method:

```js
app.use(route('/:id(\\d+)')
  .get(async ctx => {
    ctx.body = await Things.getById(ctx.params.id)
  })
  .delete(async ctx => {
    await Things.delete(ctx.params.id)
    ctx.status = 204
  })
)
```

## Maintainer

- Lead: @jonathanong [@jongleberry](https://twitter.com/jongleberry)
- Team: @koajs/routing

## API

### route(path, fns...)

`path`s are just like Express routes. `fns` is either a single middleware
or nested arrays of middleware, just like Express.

### const router = route(path)

When you don't set `fns` in the `route()` function, a router instance is returned.

### router\[method\]\(fns...\)

Define a middleware just for a specific method.

```js
app.use(route('/:id(\\d+)').get(async ctx => {
  ctx.body = await Things.getById(ctx.params.id)
}))
```

- `next` is not passed as a parameter.
  I consider this an anti-pattern in Koa - one route/method, one function.

### this.params

Any keys defined in the path will be set to `ctx.params`,
overwriting any already existing keys defined.

[npm-image]: https://img.shields.io/npm/v/koa-path-match.svg?style=flat
[npm-url]: https://npmjs.org/package/koa-path-match
[codecov-image]: https://img.shields.io/codecov/c/github/koajs/path-match/master.svg?style=flat-square
[codecov-url]: https://codecov.io/github/koajs/path-match
[license-image]: http://img.shields.io/npm/l/koa-path-match.svg?style=flat-square
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/koa-path-match.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/koa-path-match

---
_Source: https://npm.io/package/koa-path-match · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
