Skip to content

漂亮的 JSON 中间件

¥Pretty JSON Middleware

Pretty JSON 中间件为 JSON 响应主体启用 "JSON 漂亮打印"。将 ?pretty 添加到 url 查询参数,JSON 字符串将被美化。

¥Pretty JSON middleware enables "JSON pretty print" for JSON response body. Adding ?pretty to url query param, the JSON strings are prettified.

js
// GET /
{"project":{"name":"Hono","repository":"https://github.com/honojs/hono"}}

将是:

¥will be:

js
// GET /?pretty
{
  "project": {
    "name": "Hono",
    "repository": "https://github.com/honojs/hono"
  }
}

导入

¥Import

ts
import { Hono } from 'hono'
import { prettyJSON } from 'hono/pretty-json'

用法

¥Usage

ts
const app = new Hono()

app.use(prettyJSON()) // With options: prettyJSON({ space: 4 })
app.get('/', (c) => {
  return c.json({ message: 'Hono!' })
})

选项

¥Options

<徽章类型="info" 文本="optional" /> 空间:number

¥optional space: number

缩进的空格数。默认为 2

¥Number of spaces for indentation. The default is 2.

optional查询:string

¥optional query: string

用于应用的查询字符串的名称。默认为 pretty

¥The name of the query string for applying. The default is pretty.

Hono v4.7 中文网 - 粤ICP备13048890号