Registry / web-framework / koa-rewrite

koa-rewrite

JSON →
library3.0.1jsnpmunverified

URL rewrite middleware for Koa. Current version 3.0.1 supports Koa 2. Release cadence is stable but infrequent. Key differentiators: simple API using regex or route parameters similar to Express, debug support via DEBUG environment variable. Alternative libraries like koa-route provide routing only.

npm install koa-rewrite
INSTALL
IMPORT
SIG · KOA-REWRITE
K
koa-rewrite
web-frameworkjavascriptv3.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

rewrite
import rewrite from 'koa-rewrite'
const rewrite = require('koa-rewrite')
Default import; CommonJS require also works.

Basic Koa server with URL rewrite: /i123 becomes /items/123.

import Koa from 'koa'; import rewrite from 'koa-rewrite'; const app = new Koa(); app.use(rewrite(/^\/i(\w+)/, '/items/$1')); app.use(ctx => { ctx.body = ctx.url; }); app.listen(3000);
Debug
Known issues
breakingkoa-rewrite@2+ only supports koa@2; use koa-rewrite@1 for koa@1.
fix
Install koa-rewrite@1 for koa@1, or upgrade to koa@2.
affects: >=2.0.0
gotchaThe rewrite does not change the HTTP method; only URL.
fix
Use additional middleware to handle method modifications.
affects: >=1.0.0
gotchaRegex pattern must match entire path from start; otherwise unexpected matches.
fix
Anchor regex at start with ^, e.g., /^\/old(.*)/.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'koa-rewrite'
Package not installed or wrong path.
fix
Run 'npm install koa-rewrite'.
TypeError: rewrite is not a function
Incorrect import: the module exports a function but was imported as an object.
fix
Use 'import rewrite from 'koa-rewrite'' or 'const rewrite = require('koa-rewrite')'.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
koa-rewrite — npm install koa-rewrite · libregistry