Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jun 11, 2024
1 parent 7115432 commit 38b0bad
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ Create a new router.
**Example**
Basic usage:

```javascript
var Koa = require('koa');
var Router = require('@eggjs/router');
```ts
import Koa from '@eggjs/koa';
import Router from '@eggjs/router';

var app = new Koa();
var router = new Router();
const app = new Koa();
const router = new Router();

router.get('/', (ctx, next) => {
router.get('/', async (ctx, next) => {
// ctx.router available
});

Expand All @@ -77,7 +77,7 @@ where **verb** is one of the HTTP verbs such as `router.get()` or `router.post()

Additionaly, `router.all()` can be used to match against all methods.

```javascript
```ts
router
.get('/', (ctx, next) => {
ctx.body = 'Hello World!';
Expand Down Expand Up @@ -109,7 +109,7 @@ Query strings will not be considered when matching requests.
Routes can optionally have names. This allows generation of URLs and easy
renaming of URLs during development.

```javascript
```ts
router.get('user', '/users/:id', (ctx, next) => {
// ...
});
Expand All @@ -122,7 +122,7 @@ router.url('user', 3);

Multiple middleware may be given:

```javascript
```ts
router.get(
'/users/:id',
(ctx, next) => {
Expand All @@ -142,9 +142,9 @@ router.get(

Nesting routers is supported:

```javascript
var forums = new Router();
var posts = new Router();
```ts
const forums = new Router();
const posts = new Router();

posts.get('/', (ctx, next) => {...});
posts.get('/:pid', (ctx, next) => {...});
Expand All @@ -158,8 +158,8 @@ app.use(forums.routes());

Route paths can be prefixed at the router level:

```javascript
var router = new Router({
```ts
const router = new Router({
prefix: '/users'
});

Expand All @@ -171,7 +171,7 @@ router.get('/:id', ...); // responds to "/users/:id"

Named route parameters are captured and added to `ctx.params`.

```javascript
```ts
router.get('/:category/:title', (ctx, next) => {
console.log(ctx.params);
// => { category: 'programming', title: 'how-to-node' }
Expand Down Expand Up @@ -215,7 +215,7 @@ sequentially, requests start at the first middleware and work their way

**Example**

```javascript
```ts
// session middleware will run before authorize
router
.use(session())
Expand Down Expand Up @@ -244,7 +244,7 @@ Set the path prefix for a Router instance that was already initialized.

**Example**

```javascript
```ts
router.prefix('/things/:thing_id')
```

Expand All @@ -267,26 +267,26 @@ with `405 Method Not Allowed` and `501 Not Implemented` as appropriate.

**Example**

```javascript
var Koa = require('koa');
var Router = require('egg-router');
```ts
import Koa from '@eggjs/koa';
import Router from '@eggjs/router';

var app = new Koa();
var router = new Router();
const app = new Koa();
const router = new Router();

app.use(router.routes());
app.use(router.allowedMethods());
```

**Example with [Boom](https://github.com/hapijs/boom)**

```javascript
var Koa = require('koa');
var Router = require('egg-router');
var Boom = require('boom');
```ts
import Koa from '@eggjs/koa';
import Router from '@eggjs/router';
import Boom from 'boom';

var app = new Koa();
var router = new Router();
const app = new Koa();
const router = new Router();

app.use(router.routes());
app.use(router.allowedMethods({
Expand All @@ -310,7 +310,7 @@ router.redirect('/login', 'sign-in');

This is equivalent to:

```javascript
```ts
router.all('/login', ctx => {
ctx.redirect('/sign-in');
ctx.status = 301;
Expand Down Expand Up @@ -354,7 +354,7 @@ Generate URL for route. Takes a route name and map of named `params`.

**Example**

```javascript
```ts
router.get('user', '/users/:id', (ctx, next) => {
// ...
});
Expand Down Expand Up @@ -393,7 +393,7 @@ validation.

**Example**

```javascript
```ts
router
.param('user', (id, ctx, next) => {
ctx.user = users[id];
Expand Down Expand Up @@ -429,8 +429,8 @@ Generate URL from url pattern and given `params`.

**Example**

```javascript
var url = Router.url('/users/:id', {id: 1});
```ts
const url = Router.url('/users/:id', {id: 1});
// => "/users/1"

const url = Router.url('/users/:id', {id: 1}, {query: { active: true }});
Expand Down

0 comments on commit 38b0bad

Please sign in to comment.