🎀 D2 · Controller Decorators: @Get, @Post, @Body

D2 · Controller Decorators: @Get: Nest's controller decorators are like an **almost direct TypeScript translation** of Spring annotations: in Spring, `@RestController` + `@Reques

Nest's controller decorators are like an **almost direct TypeScript translation** of Spring annotations: in Spring, `@RestController` + `@RequestMapping("/bugs")` + `@GetMapping` label a class and method; in Nest, `@Controller('bugs')` + `@Get()` do the SAME job; `@RequestBody` becomes `@Body()`, `@PathVariable` becomes `@Param()`. So with such similarity, why learn Nest separately after Express? Because in Express (C2), `req.params`/`req.query` are read by hand INSIDE the function body, while in Nest this information arrives directly as a METHOD PARAMETER thanks to decorators — the contract is made visible right in the signature, just like in Spring. This is more than a "readability" preference: if a parameter is NOT MARKED with a decorator (e.g., `@Body()` is forgotten), Nest leaves that parameter `undefined` — SIMILAR to Express's `express.json()` order bug, but born from a different root cause.

The Same Endpoints, via Decorators

**🐞 Defect Birth — if the `@Body()` decorator is forgotten** **Code:** `create(body: any)` — the parameter exists, its type is written, but the `@Body()` decorator was NOT placed in front of it. **What happens:** Nest's HTTP adapter (Express) has already parsed the body, but without the decorator Nest cannot know WHICH parameter to bind that data to — the `body` parameter stays `undefined`. A `POST /api/v1/bugs { "title": "..." }` request returns 201 but the record is entirely empty. **Why sneaky:** TypeScript throws no error (`body: any` is a valid parameter), and Nest silently passes `undefined` at runtime — it LOOKS like the same result as C3's `express.json()` order bug, but its root cause is entirely different (there it was middleware order, here it is a missing decorator). **Where the tester catches it:** reading the record back with a GET after the POST and seeing all fields come back empty — the third example, in NestJS, of the "got 201 but the content is empty" family (see B1, C3).

🎬 No Decorator, No Parameter

HTTP adapter parses it

No decorator → undefined

Evidence of an empty record

The client sends a `POST /api/v1/bugs` request with a JSON body.

The HTTP adapter under Nest (Express) has already parsed the body into a JavaScript object.

ON THE CORRECT PATH: the `@Body()` decorator BINDS this parsed data to the method parameter.

IF THE DECORATOR IS MISSING: Nest cannot know which parameter to bind the data to, the parameter stays `undefined`.

The lesson — parsing and binding are two SEPARATE steps. Again, a tester does not settle for "I got 201"; they verify the returned data.

From Request to Method Parameter

The HTTP adapter parses the body/params — this is the Express layer's job.