rekam rekam - 6 months ago 66
Node.js Question

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();


app.listen(7373, function () {

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

node tmp/server.js
Route called with referer undefined
Route called with referer

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


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.