Pretty-JSON-Middleware
Die Pretty-JSON-Middleware aktiviert die formatierte JSON-Ausgabe für JSON-Antwort-Bodys. Wenn du ?pretty zu den URL-Query-Parametern hinzufügst, werden die JSON-Zeichenfolgen formatiert.
js
// GET /
{"project":{"name":"Hono","repository":"https://github.com/honojs/hono"}}Wird zu:
js
// GET /?pretty
{
"project": {
"name": "Hono",
"repository": "https://github.com/honojs/hono"
}
}Import
ts
import { Hono } from 'hono'
import { prettyJSON } from 'hono/pretty-json'Verwendung
ts
const app = new Hono()
app.use(prettyJSON()) // With options: prettyJSON({ space: 4 })
app.get('/', (c) => {
return c.json({ message: 'Hono!' })
})Optionen
optional space: number
Die Anzahl der Leerzeichen für die Einrückung. Der Standardwert ist 2.
optional query: string
Der Name des Query-Parameters zum Anwenden der Formatierung. Der Standardwert ist pretty.
optional force: boolean
Bei true werden JSON-Antworten unabhängig vom Query-Parameter immer formatiert. Der Standardwert ist false.