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?
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.
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.
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.
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.
the piped response is where the headers are being grabbed from so simply setting:
proxyReq.response = res;
fixed it.
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'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With