So my scenario is a user clicks a button on a web app, this triggers a server side POST request to an internal (i.e non public) API sitting on another server in the same network, this should return a PDF to my server which will proxy (pipe) it back to the user.
I want to just proxy the PDF body content directly to the client without creating a tmp file.
I have this code which works using the npm request module but it does not feel right:
var pdfRequest = request(requestOptions);
pdfRequest.on('error', function (err) {
utils.sendErrorResponse(500, 'PROBLEM PIPING PDF DOWNLOAD: ' + err, res);
});
pdfRequest.on('response', function (resp) {
if (resp.statusCode !== 200) {
utils.sendErrorResponse(500, 'PROBLEM PIPING PDF DOWNLOAD: RAW RESP: ' + JSON.stringify(resp), res);
} else if (resp.statusCode === 200) {
pdfRequest.pipe(res);
}
});
Is the the correct way to pipe the PDF response?
Notes:
- I need to check the status code to conditionally handle errors, the payload for the POST is contained in the requestOptions (I know this part is all correct).
- I would like to keep using the request module
- I defiantly do not want to be creating any temp files
- If possible I would also like to modify the content disposition header to set a custom filename, i know how to do this without using pipes
Aucun commentaire:
Enregistrer un commentaire