docs
This commit is contained in:
parent
288d0c00c6
commit
db0875208d
1 changed files with 8 additions and 9 deletions
|
@ -187,25 +187,24 @@ this.type = 'png';
|
||||||
Very similar to `this.request.is()`.
|
Very similar to `this.request.is()`.
|
||||||
Check whether the response type is one of the supplied types.
|
Check whether the response type is one of the supplied types.
|
||||||
This is particularly useful for creating middleware that
|
This is particularly useful for creating middleware that
|
||||||
change certain responses.
|
manipulate responses.
|
||||||
|
|
||||||
For example, this is a middleware that minifies
|
For example, this is a middleware that minifies
|
||||||
all HTML response except for streamss.
|
all HTML responses except for streams.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var minify = require('html-minifier');
|
var minify = require('html-minifier');
|
||||||
|
|
||||||
app.use(function *minifyHTML(next){
|
app.use(function *minifyHTML(next){
|
||||||
yield* next;
|
yield next;
|
||||||
|
|
||||||
if (!this.response.is('html')) return;
|
if (!this.response.is('html')) return;
|
||||||
|
|
||||||
var body = this.response.body;
|
var body = this.body;
|
||||||
if (!body) return;
|
if (!body || body.pipe) return;
|
||||||
// too difficult to do this with a stream
|
|
||||||
if ('function' == typeof body.pipe) return;
|
if (Buffer.isBuffer(body)) body = body.toString();
|
||||||
if (Buffer.isBuffer(body)) body = body.toString('utf8');
|
this.body = minify(body);
|
||||||
this.response.body = minify(body);
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue