-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: basics/middleware #194
Conversation
|
||
```js | ||
const isJSON = require('koa-is-json'); | ||
var zlib = require('zlib'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个为啥不用 const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这句 copy 的,我改一下
|
||
在框架中,我们可以完全通过配置来引入自定义的中间件,并配置它们的顺序。 | ||
|
||
如果我们需要引入上面的 gzip 中间件,在 `app/config.default.js` 中,我们加入下面的配置就完成了中间件的开启和配置: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config/config.default.js
|
||
### 配置 | ||
|
||
一般来说中间件也会有自己的配置。在框架中,一个完整的中间件是包含了配置处理的。我们约定一个中间件是一个放置在 app/middleware 目录下的单独文件,它需要 exports 一个普通的 function,接受两个参数 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以提一下中间件都挂载到 app.middlewares
上,以及为何不用 app.middleware
的原因,顺带说一下 router 中如何直接使用 middleware?
@@ -0,0 +1,158 @@ | |||
title 中间件 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
少了冒号
var zlib = require('zlib'); | ||
|
||
function* gzip(next) { | ||
if (this.method === 'HEAD') return; // HEAD 请求不响应 body,不需要进行 gzip |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
判断 accept-encoding 要支持 gzip 才行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这些逻辑我都去掉了,尽量简单
|
||
### 配置 | ||
|
||
一般来说中间件也会有自己的配置。在框架中,一个完整的中间件是包含了配置处理的。我们约定一个中间件是一个放置在 app/middleware 目录下的单独文件,它需要 exports 一个普通的 function,接受两个参数 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
app/middleware
}; | ||
``` | ||
|
||
## 在框架中引入中间件 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
框架引入的中间件是放 coreMiddleware 吧,应用中才是 middleware
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改成应用中了
|
||
## 框架默认中间件 | ||
|
||
除了应用层引入中间件之外,框架自身和其他的插件也会引入许多中间件。所有的这些自带中间件的配置项都通过在配置中修改中间件同名配置项进行修改,例如 [egg 自带的中间件](https://github.com/eggjs/egg/tree/master/app/middleware)中有一个 bodyParser 中间件(框架的加载器会将文件名中的各种分隔符都修改成驼峰形式的变量名),我们想要修改 bodyParser 的配置,只需要在 `app/config.default.js` 中编写 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config/config.default.js
}; | ||
``` | ||
|
||
**注意:框架和插件引入的中间件会在应用层配置的中间件之前,框架默认中间件不能被应用层中间件覆盖,如果应用层有定义同名中间件,在启动时会报错。** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果应用层有定义同名中间件,在启动时会报错。
如果应用层有自定义同名中间件,在启动时会报错。
Current coverage is 97.65% (diff: 100%)@@ master #194 diff @@
==========================================
Files 33 33
Lines 853 853
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
Hits 833 833
Misses 20 20
Partials 0 0
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
||
// 设置 gzip body,修正响应头 | ||
this.body = zlib.createGzip().end(body); | ||
this.set('Content-Encoding', encoding); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
encoding 这个参数在例子没有定义
No description provided.