[求助] vue 项目 vercel 部署求助

93 天前
 mbeoliero123

各位佬有用过 vercel 部署吗 根据官方文档: https://vercel.com/guides/how-to-enable-cors 在项目根路径下加了 vercel.json 重新部署后,请求后端( http://ip:port/)的 api request 没有加上跨域的 header 难道是因为我请求的是 ip ?

2455 次点击
所在节点    程序员
34 条回复
Ranhao
93 天前
我的建议是直接 vercel 转发到后端接口
mbeoliero123
93 天前
@cat #18 nuxt 和 next 都有 server 层,直接在 server 层转发就行吗?我现在想的是起个 python fastapi 服务,接收到请求就往 ip 发,拿到响应再返回
Ranhao
93 天前
加个 vercel.json
```
{
"routes": [
{ "src": "/api/(.*)", "dest": "http://xxxxxx/$1" }
]
}
```
mbeoliero123
93 天前
@Ranhao #21 大佬,是 20 楼说的这个方法吗
@thoo61871 #20 我研究下这个,感谢
cat
93 天前
@Ranhao

routes 不是返回重定向响应的么?由浏览器去请求新地址,新地址是 http 的话依然会被 block 的啊
via: https://vercel.com/docs/projects/project-configuration#routes

@mbeoliero123

你不是 vue 项目吗,你管 next 干啥 😂😂 我提到 nuxt 是因为你从 vue 迁移过去会比较简单
如果不想迁移,可以直接 /api 目录下放个文件,就是楼上说的 Functions 的玩法
参考: https://vercel.com/docs/functions#vercel-functions
Ranhao
93 天前
不是同一个,原理是一样的
20 的是要自己通过 Serverless Functions 写代码,代理请求。
21 的是可以通过 routes 配置,实现代理,类似你 vue 本地开发时的 dev proxy 。
mbeoliero123
93 天前
@cat #25 ok 我试试
Ranhao
93 天前
routes 不是返回重定向响应的么
==========================
@cat 不是,我之前配置一个代理到 tg 的都没问题,还运行得好好的,就是流量别大,可能不太符合 vercel 政策
mbeoliero123
93 天前
@Ranhao #28 我这好像不行
{
"routes": [
{
"src": "/admin/(.*)",
"dest": "http://124.220.21.162:8888/$1"
}
]
}

Ranhao
93 天前
@mbeoliero123 请求别请求 ip+port ,直接请求 /admin/login
cat
93 天前
@Ranhao 现在应该是 rewrites 写法了,我看文档是有这样一个例子:

{
"rewrites": [
{
"source": "/proxy/:match*",
"destination": "https://example.com/:match*"
}
]
}
Ranhao
93 天前
不要用 rewrites ,rewrites 是重定向,routes 才是代理,是 v2 配置的写法,现在还生效
Ranhao
93 天前
SunsetShimmer
93 天前
这个其实是个“搜索引擎是你的朋友”或者“去问 LLM”级别的问题...
既然上 Vercel 了,后端最好还是用个 CDN 套一下。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://tanronggui.xyz/t/1085714

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX