koa-better-json
TypeScript icon, indicating that this package has built-in type declarations

2.5.0 • Public • Published

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)

Readme

Keywords

none

Package Sidebar

Install

npm i koa-better-json

Weekly Downloads

367

Version

2.5.0

License

MIT

Unpacked Size

13.1 kB

Total Files

12

Last publish

Collaborators

  • damianobarbati