UNPKG

779 BMarkdownView Raw
1
2# koa-json
3
4 JSON pretty-printed response middleware.
5
6## Installation
7
8```
9$ npm install koa-json
10```
11
12## Options
13
14 - `pretty` default to pretty response [true]
15 - `param` optional query-string param for pretty responses [none]
16
17## Example
18
19 Always pretty by default:
20
21```js
22var app = koa();
23
24app.use(json());
25
26app.use(function *(next){
27 this.body = { foo: 'bar' };
28});
29```
30
31 yields:
32
33```js
34$ GET /
35
36{
37 "foo": "bar"
38}
39```
40
41 Default to being disabled (useful in production), but
42 togglable via the query-string parameter:
43
44```js
45var app = koa();
46
47app.use(json({ pretty: false, param: 'pretty' }));
48
49app.use(function *(next){
50 this.body = { foo: 'bar' };
51});
52```
53
54 yields:
55
56```js
57$ GET /
58
59{"foo":"bar"}
60```
61
62```js
63$ GET /?pretty
64
65{
66 "foo": "bar"
67}
68```
69
70# License
71
72 MIT