Appearance
方法覆盖中间件
¥Method Override Middleware
此中间件根据表单、标头或查询的值执行指定方法的处理程序(该方法与请求的实际方法不同),并返回其响应。
¥This middleware executes the handler of the specified method, which is different from the actual method of the request, depending on the value of the form, header, or query, and returns its response.
导入
¥Import
ts
import { Hono } from 'hono'
import { methodOverride } from 'hono/method-override'
用法
¥Usage
ts
const app = new Hono()
// If no options are specified, the value of `_method` in the form,
// e.g. DELETE, is used as the method.
app.use('/posts', methodOverride({ app }))
app.delete('/posts', (c) => {
// ....
})
例如
¥For example
由于 HTML 表单无法发送 DELETE 方法,你可以将值 DELETE
放在名为 _method
的属性中并发送它。app.delete()
的处理程序将被执行。
¥Since HTML forms cannot send a DELETE method, you can put the value DELETE
in the property named _method
and send it. And the handler for app.delete()
will be executed.
HTML 表单:
¥The HTML form:
html
<form action="/posts" method="POST">
<input type="hidden" name="_method" value="DELETE" />
<input type="text" name="id" />
</form>
应用:
¥The application:
ts
import { methodOverride } from 'hono/method-override'
const app = new Hono()
app.use('/posts', methodOverride({ app }))
app.delete('/posts', () => {
// ...
})
你可以更改默认值或使用标头值和查询值:
¥You can change the default values or use the header value and query value:
ts
app.use('/posts', methodOverride({ app, form: '_custom_name' }))
app.use(
'/posts',
methodOverride({ app, header: 'X-METHOD-OVERRIDE' })
)
app.use('/posts', methodOverride({ app, query: '_method' }))
选项
¥Options
required app:Hono
你的应用中使用了 Hono
的实例。
¥The instance of Hono
is used in your application.
optional 表格:string
¥optional form: string
表单键,其值包含方法名称。默认为 _method
。
¥Form key with a value containing the method name. The default is _method
.
optional 标头:boolean
¥optional header: boolean
标头名称,其值包含方法名称。
¥Header name with a value containing the method name.
optional查询:boolean
¥optional query: boolean
查询参数键,其值包含方法名称。
¥Query parameter key with a value containing the method name.