Superagent: setting header in res when piping

Created on 30 Jan 2018  路  2Comments  路  Source: visionmedia/superagent

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

All 2 comments

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



Was this page helpful?
0 / 5 - 0 ratings

Related issues

noway picture noway  路  4Comments

mikecousins picture mikecousins  路  6Comments

littlee picture littlee  路  8Comments

srohde picture srohde  路  8Comments

tbo picture tbo  路  4Comments