惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
V
V2EX
G
Google Developers Blog
F
Full Disclosure
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
NISL@THU
NISL@THU
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
A
About on SuperTechFans
The Cloudflare Blog
C
Cisco Blogs
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
Help Net Security
Help Net Security
Recorded Future
Recorded Future
PCI Perspectives
PCI Perspectives
S
Schneier on Security
AI
AI
N
News | PayPal Newsroom
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
S
Securelist
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
AWS News Blog
AWS News Blog
TaoSecurity Blog
TaoSecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 三生石上(FineUI控件)
C
CXSECURITY Database RSS Feed - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cloudbric
Cloudbric
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
Check Point Blog
S
Security Affairs

博客园 - 丁少华

Neon白嫖免费pgsql 域名利用cloudflare免费图床 nginx反代CloudflarePages提示502 vite使用shadcn vite使用biome Window下Nginx winserver2022安装不上软件 mac已损坏无法打开 代码高亮 命令运行器之task 命令运行器之just windows开启wsl 白嫖Redis 白嫖MongoDB 无服务器函数完全指南 在线 mock 方案 解释型语言和编译型语言 何时该使用monorepo monorepo前端专属吗 Cursor锁区问题 CentOS9上Let’s Encrypt自动续签 Windows给文件夹别名 ai 常识 nestjs逆向工程Prisma与DTO nestjs的orm之Prisma
vercel无服务函数
丁少华 · 2026-01-06 · via 博客园 - 丁少华

Vercel 支持无服务器函数(Serverless Functions),使用起来非常方便。以下是具体用法和配置说明:

1. 基本使用方式

无需配置的简单部署

直接在项目中创建 /api 目录,每个 .js.ts.py.go 文件都会自动成为一个 API 端点:

项目结构:
├── api/
│   ├── hello.js        → /api/hello
│   └── user/[id].js    → /api/user/:id (动态路由)
└── 其他文件

示例代码

JavaScript/TypeScript:

// api/hello.js
export default function handler(req, res) {
  res.status(200).json({ message: 'Hello World' });
}

Python:

# api/hello.py
from http.server import BaseHTTPRequestHandler

class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/plain')
        self.end_headers()
        self.wfile.write(b'Hello World')
        return

2. 配置文件(可选但推荐)

创建 vercel.json 或修改现有配置:

{
  "functions": {
    "api/*.js": {
      "maxDuration": 10,  // 最大执行时间(秒)
      "memory": 1024      // 内存大小(MB)
    }
  },
  "rewrites": [
    {
      "source": "/api/:path*",
      "destination": "/api/:path*"
    }
  ]
}

3. 环境变量配置

在 Vercel 控制台或使用 CLI:

# 设置环境变量
vercel env add VARIABLE_NAME production

# 本地开发使用
vercel env pull .env.local

4. 本地开发

# 安装 Vercel CLI
npm i -g vercel

# 登录
vercel login

# 本地启动开发服务器
vercel dev

5. 高级特性

动态路由

api/
├── post/
│   ├── [id].js         → /api/post/:id
│   └── [id]/comment.js → /api/post/:id/comment

边缘函数(Edge Functions)

更快的全局响应,在 app 目录下使用:

// app/api/route.js (Next.js 13+)
export const runtime = 'edge';

export async function GET(request) {
  return new Response('Hello from Edge!');
}

6. 部署

# 部署到生产环境
vercel --prod

# 或通过 Git 自动部署(连接 GitHub/GitLab 等)

7. 限制和注意事项

  • 免费计划限制:

    • 函数执行时间:10秒(Hobby)、15秒(Pro)
    • 内存:1024MB
    • 请求数:100GB/月带宽
  • 文件大小限制: 50MB(包括依赖)

  • 支持语言: Node.js、Python、Go、Ruby

  • 冷启动: 不活跃的函数可能有冷启动延迟

8. 调试和监控

  • Vercel 控制台: 查看函数日志和性能
  • CLI 查看日志:
    vercel logs api/hello.js
    

最简单的开始方式是直接在项目中创建 api 目录并编写函数文件,Vercel 会自动处理其余部分。

9. 结合json-server

json-server 需要持久守护进程式的服务,但是Vercel(是无服务器架构)并不支持,但是我们可以借助于其(Vercel)的Serverless之区域函数解决。
其中 kitloong的json-server-vercel是一个利用此现成的解决方案。