koa-rewrite is a specialized URL rewriting middleware designed for the Koa.js web framework. It enables developers to dynamically modify incoming request URLs based on defined patterns before they are processed by subsequent middleware or routing logic. The current stable version, 3.0.1, is compatible with Koa v2 and newer, while `koa-rewrite@1` is designated for Koa v1 applications. The library offers flexible rewrite rule definition through regular expressions, named and numeric route parameters, and wildcard matching. It provides a focused, lightweight solution for common URL manipulation needs within the Koa ecosystem, prioritizing simplicity and efficiency for path transformations rather than comprehensive routing. Releases are infrequent, primarily aligning with significant Koa framework updates.
npm install koa-rewrite-75lbVerified import paths — ran on the pinned version, not inferred.
This example sets up a basic Koa application with `koa-rewrite` to demonstrate regex, parameter-based, and wildcard URL rewriting. It uses `@koa/router` to handle the rewritten paths. To run, install `koa`, `koa-rewrite`, and `@koa/router`.
Ensure your `koa-rewrite` version matches your Koa framework version. Upgrade Koa to v2+ or use `npm install koa-rewrite@1` if on Koa v1.
Set the `DEBUG` environment variable (e.g., `DEBUG=koa-rewrite node app.js`) to see rewrite logs.
Register `app.use(rewrite(...))` statements before `app.use(router.routes())` or static middleware.
Upgrade Koa to version 2 or higher (e.g., `npm install koa@latest`) or downgrade `koa-rewrite` to version 1 (e.g., `npm install koa-rewrite@1`).
Ensure `koa-rewrite` middleware is registered early in your application's middleware stack, typically before any routing middleware (`@koa/router`) or static file serving middleware.
No dependency data recorded yet.