Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to handle a 204 No Content response in NestJS?

I'm writing a RESTful API in Nest, which I recently started using and love so far. However, I'm struggling with finding a clean pattern to handle 204 No Content responses for my GET routes. Any recommendations?

I'm a little surprised there isn't something built into the framework for returning 204 if the object returned by a GET controller method is empty -- if there is, I haven't found it yet. If there truly isn't, I'm also wondering if this is worth a feature request on GitHub.

I've tried the following:

  • Exposing the Express res property using @Response() as a Controller method parameter, then using res.sendStatus(204) if the response is empty. However, this requires me to manually send 200 responses too, whereas I'd like to still rely on the framework to handle the request-response cycle and keep my controller methods as clean as possible.
  • Looked into using an interceptor for checking if the response object is empty, then writing the 204 status code into the response there. I don't really want to do this because the status code could change later due to an exception filter.
  • Using middleware to write the response code, but my middleware executes before it gets routed to the controller and I need to check if the response is empty after that. res.on('send') didn't seem to intercept the response on its way out, either.
  • Throwing a custom NoContentException for my exception filter to handle. Although it's odd to throw an exception for a successful response code, I think this is the way I'll proceed in the meantime since my exception filter is the last thing that will execute in the code I've written.
like image 290
Eric Avatar asked Jan 03 '19 17:01

Eric


2 Answers

Update v6.1.0

Since v6.1.0 it is possible to set the response code in an Interceptor, see this PR.


Outdated answer

Unfortunately, this does not seem to be possible yet. In the docs it says:

Often, your status code isn't static but depends on various factors. In that case, you can use a library-specific response (inject using @Res()) object (or, in case of an error, throw an exception).

You also cannot just set the response code in an Interceptor without sending it (instead of sendStatus) because, as Kamil said in this thread:

global response controller's logic is the last step performed just before sending a final result through the network (that's the place where default status codes come in).

So if you (understandably) don't want to use @Res in every controller, an ExceptionFilter seems to be the best option, although it does not feel perfectly right.

Since other people seem to have the exact same problem, a feature request might be a good idea. :-)

like image 75
Kim Kern Avatar answered Sep 22 '22 12:09

Kim Kern


Looking at this example:

@Post()
@HttpCode(204)
create() {
  return 'This action adds a new cat';
}

This is all you need to do. It return's 204 for you.

like image 34
Wexoni Avatar answered Sep 18 '22 12:09

Wexoni