3.0.5 • Published 10 months ago

koa-better-json v3.0.5

Weekly downloads
37
License
MIT
Repository
github
Last release
10 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:

pnpm add koa-better-json

Usage:

import http from 'node: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)
3.0.3

10 months ago

3.0.5

10 months ago

2.6.0

11 months ago

2.5.0

2 years ago

2.4.1

2 years ago

2.4.2

2 years ago

2.4.4

2 years ago

2.3.0

4 years ago

2.2.0

4 years ago

1.1.1

5 years ago

2.0.0

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago