How to use response.download

I trying to response a pdf file to my frontend, my code:

      response.download(Helpers.tmpPath('/boletos/boleto.pdf'))
      response.type('pdf')
      response.implicitEnd = false

      boleto.end()

      boleto.pipe(response.response)

and this is the erro i receive

_http_outgoing.js:528
    throw new ERR_HTTP_HEADERS_SENT('remove');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot remove headers after they are sent to the client
    at ServerResponse.removeHeader (_http_outgoing.js:528:11)
    at clearHeaders (/Users/mauricioarantes/Documents/Projects/Nova Dublagem/dublagemBackend/node_modules/send/index.js:906:9)
    at SendStream.error (/Users/mauricioarantes/Documents/Projects/Nova Dublagem/dublagemBackend/node_modules/send/index.js:280:3)
    at SendStream.headersAlreadySent (/Users/mauricioarantes/Documents/Projects/Nova Dublagem/dublagemBackend/node_modules/send/index.js:392:8)
    at SendStream.send (/Users/mauricioarantes/Documents/Projects/Nova Dublagem/dublagemBackend/node_modules/send/index.js:618:10)
    at onstat (/Users/mauricioarantes/Documents/Projects/Nova Dublagem/dublagemBackend/node_modules/send/index.js:730:10)
    at FSReqWrap.oncomplete (fs.js:154:5)
Application crashed, make sure to kill all related running process, fix the issue and re-run the app

Why this menssage?

2 Likes

Hello @Charada32!

Really hard to say like that, but my guess is that boleto.end() ends request and then boleto.pipe() tries to add to response?

Hello @McSneaky :smiley:

.end() ends is something to PDFkit its to format the blob, the boleto.pipe i really dont know :slight_smile:

But, some news, maybe in this its more easy to help me.

Now i only use the response.download method to see if the file is donwloaded but nothing, only i receieve is a 0byte .pdf fileCaptura de Tela 2020-03-17 às 15.29.05

Haven’t dealt with v4 file download for ages, but as far as I remember you can’t return it, since as soon as you return from handler function it sends returned content to client, doing just response.download() should do it.
Or worth adding response.implicitEnd = false if first one fails