I have created a Node JS server that pushes the PDF to client as below
app.post("/home", function(req, res, next) {
// to download the file automatically
/*
var file = fs.createReadStream('E:\\test\\conversion\\310-output.pdf');
var stat = fs.statSync('E:\\test\\conversion\\310-output.pdf');
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=output.pdf');
file.pipe(res);
*/
// To Stream file to browser
var data =fs.readFileSync('E:\\test\\conversion\\310-output.pdf');
res.contentType("application/pdf");
res.send(data);
});
In client script, i am trying to call above post command and want to fetch the pdf and save it locally from response. Not sure how to do it. Below is client script. In browser, i was able to see the PDF but i need to access it through client script
var request = require('request');
request.post(
'http://localhost:3000/home',
{},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(" Response :: "+response);
// console.log(" Body :: "+body);
}
}
);