Skip to content

Commit 4fb50ac

Browse files
authoredJun 25, 2022
improve doc for prefix method. (#151)
1 parent 65414f4 commit 4fb50ac

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed
 

‎API.md

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11

22
## API Reference
33

4-
* [koa-router](#module_koa-router)
5-
* [Router](#exp_module_koa-router--Router)
6-
* [new Router([opts])](#new_module_koa-router--Router_new)
7-
* _instance_
8-
* [.get|put|post|patch|delete|del](#module_koa-router--Router+get|put|post|patch|delete|del) ⇒ <code>Router</code>
9-
* [.routes](#module_koa-router--Router+routes) ⇒ <code>function</code>
10-
* [.use([path], middleware)](#module_koa-router--Router+use) ⇒ <code>Router</code>
11-
* [.prefix(prefix)](#module_koa-router--Router+prefix) ⇒ <code>Router</code>
12-
* [.allowedMethods([options])](#module_koa-router--Router+allowedMethods) ⇒ <code>function</code>
13-
* [.redirect(source, destination, [code])](#module_koa-router--Router+redirect) ⇒ <code>Router</code>
14-
* [.route(name)](#module_koa-router--Router+route) ⇒ <code>Layer</code> &#124; <code>false</code>
15-
* [.url(name, params, [options])](#module_koa-router--Router+url) ⇒ <code>String</code> &#124; <code>Error</code>
16-
* [.param(param, middleware)](#module_koa-router--Router+param) ⇒ <code>Router</code>
17-
* _static_
18-
* [.url(path, params)](#module_koa-router--Router.url) ⇒ <code>String</code>
4+
- [API Reference](#api-reference)
5+
- [Router ⏏](#router-)
6+
- [new Router([opts])](#new-routeropts)
7+
- [router.get|put|post|patch|delete|del ⇒ <code>Router</code>](#routergetputpostpatchdeletedel--router)
8+
- [Named routes](#named-routes)
9+
- [Multiple middleware](#multiple-middleware)
10+
- [Nested routers](#nested-routers)
11+
- [Router prefixes](#router-prefixes)
12+
- [URL parameters](#url-parameters)
13+
- [router.routes ⇒ <code>function</code>](#routerroutes--function)
14+
- [router.use([path], middleware) ⇒ <code>Router</code>](#routerusepath-middleware--router)
15+
- [router.prefix(prefix) ⇒ <code>Router</code>](#routerprefixprefix--router)
16+
- [router.allowedMethods([options]) ⇒ <code>function</code>](#routerallowedmethodsoptions--function)
17+
- [router.redirect(source, destination, [code]) ⇒ <code>Router</code>](#routerredirectsource-destination-code--router)
18+
- [router.route(name) ⇒ <code>Layer</code> &#124; <code>false</code>](#routerroutename--layer--false)
19+
- [router.url(name, params, [options]) ⇒ <code>String</code> &#124; <code>Error</code>](#routerurlname-params-options--string--error)
20+
- [router.param(param, middleware) ⇒ <code>Router</code>](#routerparamparam-middleware--router)
21+
- [Router.url(path, params) ⇒ <code>String</code>](#routerurlpath-params--string)
1922

2023
<a name="exp_module_koa-router--Router"></a>
2124

@@ -224,8 +227,20 @@ Set the path prefix for a Router instance that was already initialized.
224227

225228
**Example**
226229
```javascript
227-
router.prefix('/things/:thing_id')
230+
const router = new Router({
231+
prefix: '/categories'
232+
});
233+
234+
router.get('/', ...); // respond "/categories"
235+
236+
router.prefix('/users');
237+
238+
router.get('/', ...); // responds to "/users"
239+
router.get('/:id', ...); // responds to "/users/:id"
228240
```
241+
242+
**Note**: prefix always should start from `/` otherwise it won't work.
243+
229244
<a name="module_koa-router--Router+allowedMethods"></a>
230245

231246
#### router.allowedMethods([options]) ⇒ <code>function</code>

0 commit comments

Comments
 (0)
Please sign in to comment.