Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/json
pretty-printed JSON response middleware
https://github.com/koajs/json
Last synced: 27 days ago
JSON representation
pretty-printed JSON response middleware
- Host: GitHub
- URL: https://github.com/koajs/json
- Owner: koajs
- License: mit
- Created: 2014-02-01T02:55:44.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2023-05-30T12:56:39.000Z (over 1 year ago)
- Last Synced: 2024-04-14T13:08:40.285Z (7 months ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 198
- Watchers: 4
- Forks: 12
- Open Issues: 3
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
- awesome-humanscape - koa-json - JSON pretty-printed response middleware (Node.js, koa / Utilites)
- awesome-koa - json - pretty-printed JSON response middleware (Middleware)
- awesome-koa - koa-json - 将 JSON 打印美化的中间件。 ![](https://img.shields.io/github/stars/koajs/json.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-json.svg?style=flat-square) (仓库 / 中间件)
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:
```js
const json = require('koa-json')
const Koa = require('koa')
const app = new Koa()app.use(json())
app.use((ctx) => {
ctx.body = { foo: 'bar' }
})// @request: GET /
//
// @response:
// {
// "foo": "bar"
// }
```Default to being disabled (useful in production), but togglable via the query-string parameter:
```js
const Koa = require('koa')
const app = new Koa()app.use(json({ pretty: false, param: 'pretty' }))
app.use((ctx) => {
ctx.body = { foo: 'bar' }
})// @request: GET /
//
// @response:
// {"foo":"bar"}// @request: GET /?pretty
//
// @response:
// {
// "foo": "bar"
// }
```
# License
[MIT](LICENSE)