Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/rewrite
URL rewriting middleware
https://github.com/koajs/rewrite
Last synced: 4 days ago
JSON representation
URL rewriting middleware
- Host: GitHub
- URL: https://github.com/koajs/rewrite
- Owner: koajs
- License: mit
- Created: 2013-09-09T15:24:13.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-09-16T00:22:04.000Z (3 months ago)
- Last Synced: 2024-10-29T15:32:18.174Z (about 1 month ago)
- Language: JavaScript
- Size: 21.5 KB
- Stars: 98
- Watchers: 7
- Forks: 13
- Open Issues: 4
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
- awesome-koa - rewrite - URL rewriting middleware (Middleware)
- awesome-koa - koa-rewrite - URL重写中间件。 ![](https://img.shields.io/github/stars/koajs/rewrite.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-rewrite.svg?style=flat-square) (仓库 / 中间件)
- stars - rewrite
- stars - rewrite
README
# [**koa-rewrite**](https://github.com/koajs/rewrite)
> URL rewrite middleware for koa.
___Notice: `koa-rewrite@2+` supports `koa@2`, if you want to use this module with `koa@1`, please use `koa-rewrite@1`.___
## Installation
```js
$ npm install koa-rewrite
```## Examples
Rewrite using a regular expression, rewriting `/i123` to `/items/123`.
```js
app.use(rewrite(/^\/i(\w+)/, '/items/$1'));
```Rewrite using route parameters, references may be named or numeric. For example rewrite `/foo..bar` to `/commits/foo/to/bar`:
```js
app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));
```You may also use the wildcard `*` to soak up several segments, for example `/js/vendor/jquery.js` would become `/public/assets/js/vendor/jquery.js`:
```js
app.use(rewrite('/js/(.*)', '/public/assets/js/$1'));
```## Debugging
Use the __DEBUG__ environment variable with "koa-rewrite":
```
koa-rewrite rewrite /i123 -> /items/123 +762ms
koa-rewrite rewrite /i321 -> /items/321 +9s
koa-rewrite rewrite /i123 -> /items/123 +5s
```## License
[MIT](/LICENSE)