Skip to content

Commit

Permalink
feat: Body Decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
cwy007 committed Jun 1, 2023
1 parent d472165 commit 74d12fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
24 changes: 23 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/[email protected]/dist/axios.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/qs.js"></script>
<title>static</title>
</head>
<body>
测试静态资源请求

<script>
async function urlParam() {
const res = await axios.get('/api/person/1');
console.log('Get /api/persion ->', res.data)
}
urlParam();
</script>

<script>
async function query() {
const res = await axios.get('/api/person/find', {
Expand All @@ -18,9 +27,22 @@
age: 31,
}
});
console.log('/api/person/find-->', res.data);
console.log('Get /api/person/find-->', res.data);
}
query();
</script>

<script>
async function formUrlEncoded() {
const res = await axios.post('/api/person', Qs.stringify({
name: 'cwy007',
age: 32,
}), {
headers: { 'content-type': 'application/x-www-form-urlencoded' }
});
console.log('Post /api/person-->', res.data)
}
formUrlEncoded();
</script>
</body>
</html>
5 changes: 4 additions & 1 deletion src/person/dto/create-person.dto.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export class CreatePersonDto {}
export class CreatePersonDto {
name: string;
age: number;
}
5 changes: 5 additions & 0 deletions src/person/person.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export class PersonController {
return `received: id=${id}`;
}

@Post()
body(@Body() createPersonDto: CreatePersonDto) {
return `received: ${JSON.stringify(createPersonDto)}`;
}

@Post()
create(@Body() createPersonDto: CreatePersonDto) {
return this.personService.create(createPersonDto);
Expand Down

0 comments on commit 74d12fc

Please sign in to comment.