有勇气的牛排博客

Typescript Express 02 参数获取

有勇气的牛排 300 TypeScript 2024-11-24 00:05:49

1 GET 参数

// GET 参数测试 // http://localhost:3000/api/v1/get_test?name=有勇气的牛排&id=1 router.get('/get_test', async (req: Request, res: Response) => { const {name, id} = req.query; // 从查询字符串中提取参数 const data = {id, name} ResponseUtil.success(res, data, "查询成功", 20000); });

image.png

2 POST 参数

http://127.0.0.1:3000/api/v1/post_json

json请求体

{ "username": "cs", "password": "123456" }

后端

// POST_JSON 参数测试 // http://127.0.0.1:3000/api/v1/post_json router.post('/post_json', async (req: Request, res: Response) => { const { username, password } = req.body; // 从请求体中提取参数 const data = { username, password } ResponseUtil.success(res, data, "查询成功", 20000); });

image.png

3 动态路由参数

http://127.0.0.1:3000/api/v1/user/1

// 动态路由参数 // http://127.0.0.1:3000/api/v1/user/1 router.get('/user/:id', async (req: Request, res: Response) => { const { id } = req.params; // 从动态路由中提取参数 const data = { id } ResponseUtil.success(res, data, "查询成功", 20000); });

image.png


留言

专栏
文章
加入群聊