Search code examples
node.jsexpressnode-pdfkit

Send PDF as response to client


I'm facing a strange behaviour with PdfKit. I'm using Nodejs and Express. When I call my route that generate the PDF, the route itself is called twice, and I don't understand why.

Below is the smallest code that recreate this:

var express = require('express'),
    app = express();

app.get('/', function (req, res) {
    console.log('Route called with referer', req.headers.referer);

    var PdfDocument = require('pdfkit'),
        doc = new PdfDocument();

    doc.pipe(res);
    doc.addPage();
    doc.end();
});

app.listen(7373, function () {
    console.log('started');
});

In the terminal, I have these logs, refreshing only one time the page from the browser:

node tmp/server.js
started
Route called with referer undefined
Route called with referer http://127.0.0.1:7373/

Anyone knows why the route is called one more time automatically?


Solution

  • Ok, after some analysis, I found that it's the browser's PDF viewer that launch a second call. When using wget or curl, I see only one call and one log. So just be aware that code is parsed twice when diplaying the page from the browser.