koa-json
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/koa-json package

2.0.2 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i koa-json

Weekly Downloads

95,558

Version

2.0.2

License

MIT

Last publish

Collaborators

  • coderhaoxin
  • juliangruber
  • eivifj
  • dead_horse
  • tjholowaychuk
  • jonathanong
  • jongleberry
  • fengmk2