Dav*_*vid 1 serverless cloudflare-workers hono
我从 cloudflare worker 开始,并使用推荐的路由框架 HonoJS。现在,实现 cors 功能的记录方法在我的开发机器(npm run dev)上对我不起作用。我没有在生产中测试它,因为我需要它在开发环境中工作。
问题是:OPTION 请求返回 404。
如何设置全局 CORS 配置?
我的代码目前是这样的:
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { basicAuth } from 'hono/basic-auth'
import { default as register } from './register.js'
const app = new Hono()
app.use('*', cors())
const user = new Hono()
// also tried: user.use('/*', cors())
user.post('/register', register)
// Register route groups
app.route('/user', user)
export default app
Run Code Online (Sandbox Code Playgroud)
还尝试了以下 cors 调用:
cors({
origin: 'http://localhost:5173',
allowHeaders: ['X-Custom-Header', 'Upgrade-Insecure-Requests'],
allowMethods: ['POST', 'GET', 'OPTIONS'],
exposeHeaders: ['Content-Length', 'X-Kuma-Revision'],
maxAge: 600,
credentials: true,
})
Run Code Online (Sandbox Code Playgroud)
非常感谢您的宝贵时间!
我通过添加选项通配符来修复它。
app.use('*', cors({
origin: 'http://localhost:5173',
allowHeaders: ['Content-Type', 'Authorization'],
allowMethods: ['POST', 'GET', 'OPTIONS'],
exposeHeaders: ['Content-Length'],
maxAge: 600,
credentials: true,
}))
app.options('*', (c) => {
return c.text('', 204)
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1297 次 |
| 最近记录: |