API 调用
创建 API
在 RavenSaaS 中创建新接口,跟 NextJS 框架创建新接口的规则一致。NextJS 框架 API 处理
1. 在 app/api 目录下创建一个接口文件夹,新建 route.ts 文件
2. 实现接口业务逻辑
app/api/ping/route.ts
1import { respData, respErr } from "@/lib/response";
2
3export async function POST(req: Request) {
4 try {
5 const { message } = await req.json();
6 if (!message) {
7 return respErr("invalid params");
8 }
9
10 return respData({
11 pong: `received message: ${message}`,
12 });
13 } catch (e) {
14 console.log("test failed:", e);
15 return respErr("test failed");
16 }
17}
调试 API
1. 在终端使用 curl 命令调试 API
Terminal
1curl -X POST -H "Content-Type: application/json" \
2 -d '{"message": "hello"}' \
3 http://localhost:3000/api/ping
2. 或者通过 VS Code 的 REST Client 插件调试 API
