how can I set response header when I use piping? something like this
const req = request.get('/some.json');
req.pipe(res);
// I need a event handler to set res header
req.on('response', (_res) => { res.setHeaders(_res.headers) })
I don't understand why do you set response headers. These headers are set by the server, and are supposed to be read-only for superagent.
The event handler is not supposed to allow setting response headers either. You're just abusing Node API.
I believe the OP is asking about proxying a request with superagent and setting the proxy response headers. Maybe something like:
app.get('/', (req, res) => {
const proxy = request.get('./some.json');
proxy.on('error', err => {
proxy.abort();
res.end();
...
});
proxy.on('response', resp => {
res.set(resp.headers);
...
});
proxy.pipe(res);
});