CodePark/ questions / Controllers & routing
START PRACTICINGPRACTICE
Backend question · NestJS › Controllers & routing

What happens with this handler?

@Get()
findAll(@Res() res: Response) {
  res.setHeader("X-Total", "10");
  return this.cats.findAll();
}
The request hangs, because injecting @Res() switches to library-specific mode and nothing sends a response

The request hangs. Injecting @Res() tells Nest that you will send the response yourself. Nest then ignores the return value. Nothing sends a response, so the request waits until it times out.

"Nest sends the returned array as JSON with the custom header" is wrong, because the return value is ignored in this mode.


Fixes:

• Use
@Res({ passthrough: true }). You can set headers or cookies, and Nest still sends the returned value.
• Or call
res.json() yourself.