node.jsexpressnode-request

Blank PDF with Express Request


I am getting a blank pdf when viewing in the browser and the request works because every time i search for a different pdf (localhost:3001/sample.pdf) the page numbers change, i have seen many questions asked about this and I have tried all their advice (base64 stuff, Buffer.from stuff, content-type app/pdf stuff), still haven't got it working.

app.get('/:file', function(req, res) {
    request('http://host/path/' + req.params.file, function(error, response, body) {
       res.end(body);
    });    
}).listen(port);

pls point me in right direction!


Solution

  • You aren't sending a properly defined response that has proper headers, content-type and matching encoded data. Since the server you are fetching the PDF from has already done that for you, I'd suggest you just .pipe() the response directly.

    app.get('/:file', function(req, res) {
        request('http://host/path/' + req.params.file).pipe(res);    
    }).listen(port);