Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Request Piping set response header

I am streaming a file upload in node.js and then piping the response back to the client. I want to set the content-type on the final response, but can't seem to find in the docs.

req.pipe(proxyReq).pipe(res);

I've tried

res.header('content-type', 'text/plain');

But the response gets set back to 'application/json'.

Is there good docs to how this is working and how I can set the response header?

like image 419
Dan Baker Avatar asked Feb 10 '15 20:02

Dan Baker


People also ask

How to handle HTTP headers and POST requests in node?

Now, go to terminal open the console on the console type the given command and press enter. On the successful post request the given output will appear on your screen. In this Node HTTP Headers and Post request example, we have comprehended how to set up a basic node app and Axios package to handle HTTP request.

What is response set header in Node JS?

Node.js response.setHeader () Method. The response.setHeader (name, value) (Added in v0.4.0) method is an inbuilt application programming interface of the ‘ http ‘ module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.

How do I set HTTP headers in Fred Node-RED?

Set the msg.headers field to the field value pairs of the request headers you would like to include in the message sent to the HTTP request node. Example. In this example we set the X-Auth-User and X-Auth-Key request headers to call a private HTTP input node on the FRED Node-RED cloud service.

What is the use of setheader in Node JS?

Node.js response.setHeader () Method Last Updated : 15 Sep, 2020 The response.setHeader (name, value) (Added in v0.4.0) method is an inbuilt application programming interface of the ‘ http ‘ module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.


2 Answers

the piped response is where the headers are being grabbed from so simply setting:

proxyReq.response = res;

fixed it.

like image 90
Dan Baker Avatar answered Nov 04 '22 14:11

Dan Baker


For anyone still experiencing this problem. This is how I fixed it:

  const x = _request.pipe(proxyReq);
  x.end();

  x.on("response",(res)=>{
    const routedResp = res.pipe(response);
    routedResp.setHeader("content-type","application/json");
  })

_request and response being the middleware passed parameters. request being 'http.request'.

like image 27
izSaad Avatar answered Nov 04 '22 13:11

izSaad