HonoRequest
HonoRequest 是一个可以从 c.req 获取的对象,它封装了一个 Request 对象。
param()
获取路径参数的值。
ts
// Captured params
app.get('/entry/:id', async (c) => {
const id = c.req.param('id')
// ...
})
// Get all params at once
app.get('/entry/:id/comment/:commentId', async (c) => {
const { id, commentId } = c.req.param()
})query()
获取查询字符串参数。
ts
// Query params
app.get('/search', async (c) => {
const query = c.req.query('q')
})
// Get all params at once
app.get('/search', async (c) => {
const { q, limit, offset } = c.req.query()
})queries()
获取多个查询字符串参数值,例如 /search?tags=A&tags=B
ts
app.get('/search', async (c) => {
// tags will be string[]
const tags = c.req.queries('tags')
// ...
})header()
获取请求头值。
ts
app.get('/', (c) => {
const userAgent = c.req.header('User-Agent')
return c.text(`Your user agent is ${userAgent}`)
})警告
当 c.req.header() 不带任何参数调用时,返回记录中的所有键都将小写。
如果您要获取名称为大写的头的值,请使用 c.req.header(“X-Foo”)。
ts
// ❌ Will not work
const headerRecord = c.req.header()
const foo = headerRecord['X-Foo']
// ✅ Will work
const foo = c.req.header('X-Foo')parseBody()
解析类型为 multipart/form-data 或 application/x-www-form-urlencoded 的请求主体。
ts
app.post('/entry', async (c) => {
const body = await c.req.parseBody()
// ...
})parseBody() 支持以下行为。
单个文件
ts
const body = await c.req.parseBody()
const data = body['foo']
body['foo'] 是 (string | File)。
如果上传了多个文件,将使用最后一个文件。
多个文件
ts
const body = await c.req.parseBody()
body['foo[]']body['foo[]'] 始终为 (string | File)[]。
需要 [] 后缀。
具有相同名称的多个文件
ts
const body = await c.req.parseBody({ all: true })
body['foo']默认情况下禁用 all 选项。
- 如果
body['foo']是多个文件,它将被解析为(string | File)[]。 - 如果
body['foo']是单个文件,它将被解析为(string | File)。
点表示法
如果您将 dot 选项设置为 true,则返回值将基于点表示法进行结构化。
假设接收以下数据
ts
const data = new FormData()
data.append('obj.key1', 'value1')
data.append('obj.key2', 'value2')您可以通过将 dot 选项设置为 true 来获取结构化值
ts
const body = await c.req.parseBody({ dot: true })
// body is `{ obj: { key1: 'value1', key2: 'value2' } }`json()
解析类型为 application/json 的请求主体
ts
app.post('/entry', async (c) => {
const body = await c.req.json()
// ...
})text()
解析类型为 text/plain 的请求主体
ts
app.post('/entry', async (c) => {
const body = await c.req.text()
// ...
})arrayBuffer()
将请求主体解析为 ArrayBuffer
ts
app.post('/entry', async (c) => {
const body = await c.req.arrayBuffer()
// ...
})blob()
将请求主体解析为 Blob。
ts
app.post('/entry', async (c) => {
const body = await c.req.blob()
// ...
})formData()
将请求主体解析为 FormData。
ts
app.post('/entry', async (c) => {
const body = await c.req.formData()
// ...
})valid()
获取验证后的数据。
ts
app.post('/posts', async (c) => {
const { title, body } = c.req.valid('form')
// ...
})以下是可以使用的目标。
表格json查询标题cookie参数
有关使用示例,请参见 验证部分。
routePath()
您可以在处理程序中像这样检索注册的路径
ts
app.get('/posts/:id', (c) => {
return c.json({ path: c.req.routePath })
})如果您访问 /posts/123,它将返回 /posts/:id
json
{ "path": "/posts/:id" }matchedRoutes()
它返回处理程序中匹配的路由,这对于调试很有用。
ts
app.use(async function logger(c, next) {
await next()
c.req.matchedRoutes.forEach(({ handler, method, path }, i) => {
const name =
handler.name ||
(handler.length < 2 ? '[handler]' : '[middleware]')
console.log(
method,
' ',
path,
' '.repeat(Math.max(10 - path.length, 0)),
name,
i === c.req.routeIndex ? '<- respond from here' : ''
)
})
})路径
请求路径名。
ts
app.get('/about/me', async (c) => {
const pathname = c.req.path // `/about/me`
// ...
})URL
请求 URL 字符串。
ts
app.get('/about/me', async (c) => {
const url = c.req.url // `https://:8787/about/me`
// ...
})方法
请求的方法名称。
ts
app.get('/about/me', async (c) => {
const method = c.req.method // `GET`
// ...
})原始
原始的 Request 对象。
ts
// For Cloudflare Workers
app.post('/', async (c) => {
const metadata = c.req.raw.cf?.hostMetadata?
// ...
})