Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/koajs/json

pretty-printed JSON response middleware
https://github.com/koajs/json

Last synced: 27 days ago
JSON representation

pretty-printed JSON response middleware

Awesome Lists containing this project

README

        

# koa-json

JSON pretty-printed response middleware. Also converts node object streams to binary.

## Installation

```
$ npm install koa-json
```

## Options

- `pretty` default to pretty response [true]
- `param` optional query-string param for pretty responses [none]
- `spaces` JSON spaces [2]

## Example

Always pretty by default:

```js
const json = require('koa-json')
const Koa = require('koa')
const app = new Koa()

app.use(json())

app.use((ctx) => {
ctx.body = { foo: 'bar' }
})

// @request: GET /
//
// @response:
// {
// "foo": "bar"
// }
```

Default to being disabled (useful in production), but togglable via the query-string parameter:

```js
const Koa = require('koa')
const app = new Koa()

app.use(json({ pretty: false, param: 'pretty' }))

app.use((ctx) => {
ctx.body = { foo: 'bar' }
})

// @request: GET /
//
// @response:
// {"foo":"bar"}

// @request: GET /?pretty
//
// @response:
// {
// "foo": "bar"
// }
```

# License

[MIT](LICENSE)