“Can't set headers after they are sent” - when setting response directly





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm using NestJS for my API server and am very satisfied with it.
Most of my controller functions are calling async functions and return JSON - works like a charm.



But in some controller functions, i need to set the response/headers/etc directly to the response object, e.g.:



@Get('api/media')
async getMedia(@Param('id') id: string,
@Req() req) {
let result = await getMediaFromBackend(id);
req.res.set('Content-Type', result.contentType); // need to set content-type dynamically
req.res.send(result.data); // send non-json response
}


In some other case i need to pipe a gzipStream.



It works, but i'm always getting an unhandled rejection in the console:




"Can't set headers after they are sent"




Seems like NestJS wants to set/overwrite Headers after the controller function returns.
Any ideas how to avoid this? I would need some functionality to tell NestJS like "im taking full care of the response myself"



Thanks a lot!










share|improve this question































    1















    I'm using NestJS for my API server and am very satisfied with it.
    Most of my controller functions are calling async functions and return JSON - works like a charm.



    But in some controller functions, i need to set the response/headers/etc directly to the response object, e.g.:



    @Get('api/media')
    async getMedia(@Param('id') id: string,
    @Req() req) {
    let result = await getMediaFromBackend(id);
    req.res.set('Content-Type', result.contentType); // need to set content-type dynamically
    req.res.send(result.data); // send non-json response
    }


    In some other case i need to pipe a gzipStream.



    It works, but i'm always getting an unhandled rejection in the console:




    "Can't set headers after they are sent"




    Seems like NestJS wants to set/overwrite Headers after the controller function returns.
    Any ideas how to avoid this? I would need some functionality to tell NestJS like "im taking full care of the response myself"



    Thanks a lot!










    share|improve this question



























      1












      1








      1








      I'm using NestJS for my API server and am very satisfied with it.
      Most of my controller functions are calling async functions and return JSON - works like a charm.



      But in some controller functions, i need to set the response/headers/etc directly to the response object, e.g.:



      @Get('api/media')
      async getMedia(@Param('id') id: string,
      @Req() req) {
      let result = await getMediaFromBackend(id);
      req.res.set('Content-Type', result.contentType); // need to set content-type dynamically
      req.res.send(result.data); // send non-json response
      }


      In some other case i need to pipe a gzipStream.



      It works, but i'm always getting an unhandled rejection in the console:




      "Can't set headers after they are sent"




      Seems like NestJS wants to set/overwrite Headers after the controller function returns.
      Any ideas how to avoid this? I would need some functionality to tell NestJS like "im taking full care of the response myself"



      Thanks a lot!










      share|improve this question
















      I'm using NestJS for my API server and am very satisfied with it.
      Most of my controller functions are calling async functions and return JSON - works like a charm.



      But in some controller functions, i need to set the response/headers/etc directly to the response object, e.g.:



      @Get('api/media')
      async getMedia(@Param('id') id: string,
      @Req() req) {
      let result = await getMediaFromBackend(id);
      req.res.set('Content-Type', result.contentType); // need to set content-type dynamically
      req.res.send(result.data); // send non-json response
      }


      In some other case i need to pipe a gzipStream.



      It works, but i'm always getting an unhandled rejection in the console:




      "Can't set headers after they are sent"




      Seems like NestJS wants to set/overwrite Headers after the controller function returns.
      Any ideas how to avoid this? I would need some functionality to tell NestJS like "im taking full care of the response myself"



      Thanks a lot!







      javascript node.js typescript api nestjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 13:36









      Kim Kern

      11.1k53352




      11.1k53352










      asked Nov 22 '18 at 8:36









      jueschusjueschus

      185




      185
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Because you're injecting @Req() and access the response via req.res the standard nest controller behavior is used. If you directly use @Res instead nest won't mess with the response object and you have full control over it.



          @Get('api/media')
          async getMedia(@Param('id') id: string,
          @Res() res) {
          let result = await getMediaFromBackend(id);
          res.set('Content-Type', result.contentType); // need to set content-type dynamically
          res.send(result.data); // send non-json response
          }





          share|improve this answer
























          • thanks a lot - that solved my issue. i remember some post where i read that with req.res you can get the original response object - which also makes total sense now.

            – jueschus
            Nov 22 '18 at 15:28














          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426796%2fcant-set-headers-after-they-are-sent-when-setting-response-directly%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









          1














          Because you're injecting @Req() and access the response via req.res the standard nest controller behavior is used. If you directly use @Res instead nest won't mess with the response object and you have full control over it.



          @Get('api/media')
          async getMedia(@Param('id') id: string,
          @Res() res) {
          let result = await getMediaFromBackend(id);
          res.set('Content-Type', result.contentType); // need to set content-type dynamically
          res.send(result.data); // send non-json response
          }





          share|improve this answer
























          • thanks a lot - that solved my issue. i remember some post where i read that with req.res you can get the original response object - which also makes total sense now.

            – jueschus
            Nov 22 '18 at 15:28


















          1














          Because you're injecting @Req() and access the response via req.res the standard nest controller behavior is used. If you directly use @Res instead nest won't mess with the response object and you have full control over it.



          @Get('api/media')
          async getMedia(@Param('id') id: string,
          @Res() res) {
          let result = await getMediaFromBackend(id);
          res.set('Content-Type', result.contentType); // need to set content-type dynamically
          res.send(result.data); // send non-json response
          }





          share|improve this answer
























          • thanks a lot - that solved my issue. i remember some post where i read that with req.res you can get the original response object - which also makes total sense now.

            – jueschus
            Nov 22 '18 at 15:28
















          1












          1








          1







          Because you're injecting @Req() and access the response via req.res the standard nest controller behavior is used. If you directly use @Res instead nest won't mess with the response object and you have full control over it.



          @Get('api/media')
          async getMedia(@Param('id') id: string,
          @Res() res) {
          let result = await getMediaFromBackend(id);
          res.set('Content-Type', result.contentType); // need to set content-type dynamically
          res.send(result.data); // send non-json response
          }





          share|improve this answer













          Because you're injecting @Req() and access the response via req.res the standard nest controller behavior is used. If you directly use @Res instead nest won't mess with the response object and you have full control over it.



          @Get('api/media')
          async getMedia(@Param('id') id: string,
          @Res() res) {
          let result = await getMediaFromBackend(id);
          res.set('Content-Type', result.contentType); // need to set content-type dynamically
          res.send(result.data); // send non-json response
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 13:35









          Kim KernKim Kern

          11.1k53352




          11.1k53352













          • thanks a lot - that solved my issue. i remember some post where i read that with req.res you can get the original response object - which also makes total sense now.

            – jueschus
            Nov 22 '18 at 15:28





















          • thanks a lot - that solved my issue. i remember some post where i read that with req.res you can get the original response object - which also makes total sense now.

            – jueschus
            Nov 22 '18 at 15:28



















          thanks a lot - that solved my issue. i remember some post where i read that with req.res you can get the original response object - which also makes total sense now.

          – jueschus
          Nov 22 '18 at 15:28







          thanks a lot - that solved my issue. i remember some post where i read that with req.res you can get the original response object - which also makes total sense now.

          – jueschus
          Nov 22 '18 at 15:28






















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426796%2fcant-set-headers-after-they-are-sent-when-setting-response-directly%23new-answer', 'question_page');
          }
          );

          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







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?