一个基于 NestJS 的 CORS 代理服务,用于解决前端跨域问题,可部署到 Vercel。
- 🚀 支持所有 HTTP 方法 (GET, POST, PUT, DELETE 等)
- 🔒 自动处理 CORS 头部
- 📦 支持 JSON 和文本响应
- ⚡ 基于 NestJS 框架,支持 TypeScript
- 🛡️ 基本的 URL 验证和错误处理
https://your-vercel-domain.vercel.app/api/proxy?url=目标URL
// 原本跨域的请求
fetch('https://api.example.com/data') // ❌ 跨域错误
// 使用代理服务
fetch('https://your-proxy.vercel.app/api/proxy?url=https://api.example.com/data') // ✅ 成功fetch('https://your-proxy.vercel.app/api/proxy?url=https://api.example.com/users')fetch('https://your-proxy.vercel.app/api/proxy?url=https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name: 'John', email: 'john@example.com' })
})- 安装 Vercel CLI
npm i -g vercel- 构建项目
npm run build- 在项目目录中运行
vercel- 按照提示完成部署
- 将代码推送到 GitHub 仓库
- 在 Vercel Dashboard 中导入项目
- 选择你的 GitHub 仓库
- 点击部署
- 安装依赖
npm install- 启动开发服务器
npm run start:dev- 访问
http://localhost:3000/api/proxy?url=目标URL
- 构建项目
npm run build- 启动生产服务器
npm run start:prod服务会返回以下错误信息:
400: 缺少 url 参数或 URL 格式无效500: 代理请求失败
- 添加域名白名单限制
- 实现请求频率限制
- 添加身份验证
- 监控和日志记录
MIT License