Skip to content Skip to sidebar Skip to footer

Send A Pdf Generated From An Api To An External Server?

I have a NodeJS-ExpressJS server (to host a web page) and Django API server. When a specific endpoint in the Django server is called from the ExpressJS server (when a user clicks a

Solution 1:

I think your code is bulky and buffers up the entire file.pdf into memory (body) for every request before writing the result back to clients, it could start eating a lot of memory if there were many requests at the same time, that's why you should use streams:

server.get('/apisd', function(req, res, next) {

    // Force browser to download file
    res.set('Content-Type', 'application/pdf');
    res.set('Content-Disposition', 'attachment; filename=file.pdf');

    // send file
    request.post({
        url: 'http://<api_url>',
        oauth: oauth,
        preambleCRLF: true,
        postambleCRLF: true
    }).pipe(res);

});

By using streams the program will read file.pdf one chunk at a time, store it into memory and send it back to the client.

Post a Comment for "Send A Pdf Generated From An Api To An External Server?"