2.5.0 • Published 12 months ago

koa-better-json v2.5.0

Weekly downloads
37
License
MIT
Repository
github
Last release
12 months ago

koa-better-json

Koa middleware returning JSON encoded response.

The middleware will encode only:

  • pathname having an empty or .json extension
  • encodable body

Why not simply use the most common koa-json? koa-json has the following behaviours / limitations:

  • you can't return a null payload (eg: ctx.body = null) as this results in a 204 empty response
  • you can't return a plain string payload (eg: `ctx.body = 'hello there') as this results in plain text response

If you need to send valid null and plain values response then this middleware is for you.

Usage

Install:

yarn add koa-better-json

Usage:

import http from 'http';
import koa from 'koa';
import json from 'koa-better-json';

const app = new koa()
app.use(json());
app.use(ctx => ctx.body = { foo: 'bar' });
app.use(ctx => ctx.body = null);
app.use(ctx => ctx.body = 'abc');

const server = http
  .createServer(app.callback())
  .listen(8080, console.log);

Options

  • pretty default to pretty response (default to true)
  • spaces JSON spaces (default to 2)
  • replacer JSON replacer (default to undefined)
2.5.0

12 months ago

2.4.1

1 year ago

2.4.2

1 year ago

2.4.4

1 year ago

2.3.0

3 years ago

2.2.0

3 years ago

1.1.1

4 years ago

2.0.0

4 years ago

1.1.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago