Package detail

koa-json

koajs469.4kMIT2.0.2

pretty (non-compressed) json response middleware

koa, json

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:

var json = require('koa-json');
var Koa = require('koa');
var 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:

var Koa = require('koa');
var 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

changelog

2.0.1 / 2016-04-28

  • fix: compatibility with node 6
  • travis: add node 6

2.0.0 / 2016-03-27

  • update to koa v2

1.1.0 / 2014-05-28

  • object stream support

1.0.1 / 2014-05-03

  • fix param-based pretty formatting

1.0.0 / 2014-05-02

  • add: .spaces option
  • refactor: use koa-is-json