What's the best way to handle a 204 No Content response in NestJS?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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 usingres.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.
typescript rest api-design nestjs
add a comment |
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 usingres.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.
typescript rest api-design nestjs
add a comment |
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 usingres.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.
typescript rest api-design 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 usingres.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.
typescript rest api-design nestjs
typescript rest api-design nestjs
edited Jan 3 at 18:57


Kim Kern
11.5k53457
11.5k53457
asked Jan 3 at 17:01
EricEric
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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. :-)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026658%2fwhats-the-best-way-to-handle-a-204-no-content-response-in-nestjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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. :-)
add a comment |
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. :-)
add a comment |
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. :-)
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. :-)
answered Jan 3 at 18:56


Kim KernKim Kern
11.5k53457
11.5k53457
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026658%2fwhats-the-best-way-to-handle-a-204-no-content-response-in-nestjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown