Skip to content

漂亮的 JSON 中间件

🌐 Pretty JSON Middleware

Pretty JSON 中间件为 JSON 响应体启用“JSON 美化打印”。向 URL 查询参数添加 ?pretty,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

optional 空间: number

缩进的空格数。默认值是 2

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

optional 查询:string

用于申请的查询字符串名称。默认值是 pretty

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

optional 力:boolean

当设置为 true 时,无论查询参数如何,JSON 响应总是美化的。默认值为 false

🌐 When set to true, JSON responses are always prettified regardless of the query parameter. The default is false.

Hono 中文网 - 粤ICP备13048890号