Appearance
CBOR
CBOR 是一种用于序列化 RFC 8949 中定义的对象的二进制格式。它兼容 JSON,适用于需要高效数据交换的网络通信,以及资源受限的环境,例如物联网设备。
¥CBOR is a binary format for serializing objects defined in RFC 8949. It is JSON-compatible and suitable for network communications that require efficient data exchange, as well as for use in resource-constrained environments such as IoT devices.
以下是使用 cbor2 包以 CBOR 方式响应的示例:
¥Here's an example of using cbor2 package to respond with CBOR:
ts
import { Hono } from 'hono'
import { createMiddleware } from 'hono/factory'
import { encode } from 'cbor2'
const app = new Hono()
declare module 'hono' {
interface ContextRenderer {
(content: any): Response | Promise<Response>
}
}
const cborRenderer = createMiddleware(async (c, next) => {
c.header('Content-Type', 'application/cbor')
c.setRenderer((content) => {
return c.body(encode(content))
})
await next()
})
app.use(cborRenderer)
app.get('/', (c) => {
return c.render({ message: 'hello CBOR!' })
})
export default app你可以使用以下命令检查响应。
¥You can check the response using the following command.
plaintext
$ curl -s http://localhost:3000/ | hexdump -C
00000000 a1 67 6d 65 73 73 61 67 65 6b 68 65 6c 6c 6f 20 |.gmessagekhello |
00000010 43 42 4f 52 21 |CBOR!|
00000015此外,你还可以在 CBOR Playground 中验证它是否解码为 JSON 对象。
¥Additionally, you can verify that it decodes to a JSON object in the CBOR playground.
plaintext
A1 # map(1)
67 # text(7)
6D657373616765 # "message"
6B # text(11)
68656C6C6F2043424F5221 # "hello CBOR!"json
{ "message": "hello CBOR!" }另请参阅
¥See also