All Projects → koajs → Json

koajs / Json

pretty-printed JSON response middleware

Programming Languages

javascript
184084 projects - #8 most used programming language

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].