I use got to proxy request, first I had a duplex stream by calling got.stream
module.exports = (req, res) => {
const s = got.stream(TARGET_URL)
}
Then I listen for response event to got the target response obj of the final request, I use it to set whatever header the response obj had. ( proxy )
s.on('response', (targetResponse) => {
res.statusCode = targetResponse.statusCode
Object.keys(targetResponse.headers).forEach(key => {
res.setHeader(key, targetResponse.headers[key])
})
})
and finally pipe the duplex stream to my client response.
s.pipe(res)
The problem was sometime response event was slow and the header already sent, that cause Error: Can't set headers after they are sent.
My question is:
Is there anyway to use pipe and able to set header ?
Problem is - you start sending res before response. res should explicitly wait for response event and only after call send.
I did wait for response with the following code
const s = got.stream(TARGET_URL, { headers })
s
.on('error', handleError)
.on('response', (response) => {
res.statusCode = response.statusCode
Object.keys(response.headers).forEach(key => {
res.setHeader(key, response.headers[key])
})
s.pipe(res)
})
but still got error Error: Can't set headers after they are sent. not sure what I did wrong in these lines of code.
Also I don't get the part only after call send what does it mean ?
Also I don't get the part only after call send what does it mean ?
@quocnguyen res.send sends headers and body (so does s.pipe(res)) and this is cause of error.
This code looks fine, can you post example, that reproduces that problem?
I think my problem is when the underlying res closed, got duplexer stream still continue buffer up and pipe to res. I will set up a example code when I got free time. I solve it by abort request when res close event emit.
Thank you for this great lib, I learn a lot about duplexer stream and stuffs through got. 馃憤