Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zskycat/koa-path-mapping
用于对请求进行重定向或路径重写的 koa 中间件,支持对失败的请求进行路径重写。
https://github.com/zskycat/koa-path-mapping
error koa-middleware redirect rewrite
Last synced: 24 days ago
JSON representation
用于对请求进行重定向或路径重写的 koa 中间件,支持对失败的请求进行路径重写。
- Host: GitHub
- URL: https://github.com/zskycat/koa-path-mapping
- Owner: ZSkycat
- License: mit
- Created: 2018-08-12T18:51:13.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-08-24T18:27:35.000Z (about 6 years ago)
- Last Synced: 2024-10-15T17:31:24.035Z (24 days ago)
- Topics: error, koa-middleware, redirect, rewrite
- Language: TypeScript
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# koa-path-mapping
[![version](https://img.shields.io/npm/v/koa-path-mapping.svg)](https://www.npmjs.com/package/koa-path-mapping)
[![downloads](https://img.shields.io/npm/dt/koa-path-mapping.svg)](https://www.npmjs.com/package/koa-path-mapping)Koa middleware for redirect or path rewrite of requests. Support path rewrite of failed requests.
用于对请求进行重定向或路径重写的 koa 中间件,支持对失败的请求进行路径重写。允许对 404 请求进行路径重写,并回退到 `next()`,对于以下场景非常有用:
- 使用 `HTML5 History API` 的单页应用和多页应用
- 使用 `webpack-dev-middleware` 的开发服务器,例如 `webpack-serve`## 起步
**安装**
```
npm install --save-dev koa-path-mapping
```**用法**
```javascript
const Koa = require('koa');
const pathMappingMiddleware = require('koa-path-mapping').pathMappingMiddleware;const app = new Koa();
const options = {
mapping: [
{ action: 'redirect', from: '^/$', to: '/login' },
{ action: 'rewrite', from: '^/game($|/)', to: '/app-game.html' },
],
error: [{ action: 'rewrite', status: 404, to: '/app.html' }],
};
app.use(pathMappingMiddleware(options));
```## API
```javascript
pathMappingMiddleware(options: PathMappingOptions)
```### PathMappingOptions
创建中间件的选项。### PathMappingOptions.mapping
**类型:** `MappingOptions[]`
**默认:** `[]`
[参考](#mappingoptions)### PathMappingOptions.error
**类型:** `ErrorOptions[]`
**默认:** `[]`
[参考](#erroroptions)### PathMappingOptions.ignoreJson
**类型:** `boolean`
**默认:** `true`
是否忽略 `application/json` 的请求。### PathMappingOptions.enableLog
**类型:** `boolean`
**默认:** `false`
是否输出日志。### PathMappingOptions.logger
**类型:** `Function`
**默认:** `console.log`
设置输出日志所使用的函数。---
### MappingOptions
对请求进行重定向或路径重写,在进入时执行。### MappingOptions.action
**类型:** `'redirect' | 'rewrite'`
设置执行的动作,必填。### MappingOptions.from
**类型:** `RegExp | string`
设置匹配的 `path`,如果未设置,则匹配所有的请求。值要求为 `RegExp` 实例,或是 `RegExp` 格式的字符串。### MappingOptions.to
**类型:** `string`
设置目标的 `path`,必填。---
### ErrorOptions
对失败的请求进行重定向或路径重写,在 `next()` 之后执行。如果匹配成功并执行路径重写,那么会再一次执行 `next()`。### ErrorOptions.action
**类型:** `'redirect' | 'rewrite'`
设置执行的动作,必填。### ErrorOptions.status
**类型:** `number`
设置匹配的状态码,如果未设置,则匹配 `status >= 400`。### ErrorOptions.from
**类型:** `RegExp | string`
设置匹配的 `path`,如果未设置,则匹配所有的请求。值要求为 `RegExp` 实例,或是 `RegExp` 格式的字符串。### ErrorOptions.to
**类型:** `string`
设置目标的 `path`,必填。## License
[MIT](https://github.com/ZSkycat/koa-path-mapping/blob/master/LICENSE.txt)