pretty-printed JSON response middleware
JavaScript
Clone or download
Latest commit 8b4bb53 Apr 16, 2017
Permalink
Failed to load latest commit information.
examples update examples Apr 16, 2017
test tests ++ Apr 16, 2017
.eslintrc lint Apr 16, 2017
.gitignore add coverage Sep 16, 2014
.travis.yml travis: node@7 only Apr 16, 2017
History.md 2.0.1 Apr 28, 2016
Readme.md use const Apr 16, 2017
index.js lint Apr 16, 2017
package.json lint Apr 16, 2017

Readme.md

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:

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

app.use(json())

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

yields:

$ GET /

{
  "foo": "bar"
}

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

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

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

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

yields:

$ GET /

{"foo":"bar"}
$ GET /?pretty

{
  "foo": "bar"
}

License

MIT