Skip to content

Commit

Permalink
docs: update use middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Aug 3, 2023
1 parent 0d8ebc5 commit 53c2f0f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 5 deletions.
47 changes: 47 additions & 0 deletions site/docs/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ export class MainConfiguration {
这样的话,社区很多 koa 三方中间件都可以比较方便的接入。



## 使用社区中间件


我们以 `koa-static` 举例。


Expand All @@ -337,6 +341,49 @@ async onReady() {
}
```

如果中间件支持在路由上引入,比如:

```typescript
const Koa = require('koa');
const app = new Koa();
app.get('/controller', require('koa-static')(root, opts));
```

我们也可以将中间件看成普通函数,放在装饰器参数中。

```typescript
const staticMiddleware = require('koa-static')(root, opts);

// ...
class HomeController {
@Get('/controller', {middleware: [staticMiddleware]})
async getMethod() {
// ...
}
}
```

也可以作为作为路由方法体使用。

```typescript
const staticMiddleware = require('koa-static')(root, opts);

// ...
class HomeController {
@Get('/controller')
async getMethod(ctx, next) {
// ...
return staticMiddleware(ctx, next);
}
}
```

:::tip

三方中间件写法有很多种,上面只是列出最基本的使用方式。

:::



## 获取中间件名
Expand Down
57 changes: 52 additions & 5 deletions site/i18n/en/docusaurus-plugin-content-docs/current/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,26 +314,73 @@ export class MainConfiguration {
In this way, many koa tripartite middleware in the community can be easily accessed.


Take `koa-static` as an example.

## Use community middleware

In the `koa-static` document, it is written as follows.

Let's take `koa-static` as an example.


In the `koa-static` documentation, it is written like this.

```typescript
const Koa = require('koa');
const app = new Koa();
app.use(require('koa-static')(root, opts));
```

Then, the `require('koa-static'))(root, opts)` is actually the returned middleware method. We can export it directly and call `useMiddleware`.
Then, `require('koa-static')(root, opts)` is actually the returned middleware method, we can export it directly and call `useMiddleware`.

```typescript
async onReady() {
// add middleware
this.app.useMiddleware(require('koa-static')(root, opts));
// add middleware
this.app.useMiddleware(require('koa-static')(root, opts));
}
```

If the middleware supports importing on routes, for example:

```typescript
const Koa = require('koa');
const app = new Koa();
app.get('/controller', require('koa-static')(root, opts));
```

We can also treat middleware as ordinary functions and place them in decorator parameters.

```typescript
const static Middleware = require('koa-static')(root, opts);

//...
class HomeController {
@Get('/controller', {middleware: [staticMiddleware]})
async getMethod() {
//...
}
}
```

It can also be used as a routing method body.

```typescript
const static Middleware = require('koa-static')(root, opts);

//...
class HomeController {
@Get('/controller')
async getMethod(ctx, next) {
//...
return static Middleware(ctx, next);
}
}
```

:::tip

There are many ways to write three-party middleware, and the above are just the most basic ways to use it.

:::



## Get the middleware name
Expand Down

0 comments on commit 53c2f0f

Please sign in to comment.