I have made an endpoint in my expressJs application, which should be able to proxy a large raw binary to a remote backend rest service, without saving anything to memory.
I can see that this relatively easily can be accomplished with request, by doing something like this:
req.pipe(request({method: "POST"}))
However, since the request library is deprecated, I want to use fetch instead. Thus far, I have come up with something as follows:
app.post("/my-endpoint", async (req, res) => {
try {
const url = http://link-to-backend.com/
const request = await fetch(url, {
method: "POST",
body: req.body
});
request.body.pipe(res);
} catch (e) {
res.status(500).send("error");
}
});
Above works as expected. However based on above, I would like to know if my approach actually saves req.body
into memory before proxying the request to the backend api, and how I effectively can validate if it does/doesn't.
CodePudding user response:
The following middlewares passes the request and response bodies through chunk by chunk, without storing them entirely in memory:
app.post("/my-endpoint", async (req, res, next) => {
delete req.headers.host; // you may want to delete certain cookies as well
req.pipe(http.request(url, {
method: "POST",
headers: req.headers
}).on("response", function(r) {
res.writeHead(r.statusCode, r.statusMessage, r.headers);
r.pipe(res);
}).on("error", next));
});