Search code examples
javascriptnode.jsexpressnode.js-connect

How to use Q promises in ExpressJS?


I need to generate pdf docs. All magic about pdf going in generateDoc func which return promise with Buffer data as a param. But Express doesn't send data to the client, only header. What am I doing wrong?

app.get('/', function(req, res) {
        generateDoc().then(function(data) {
            res.set({
                'Content-Type': 'application/pdf',
                'Content-Length': data.length
            });
            res.end(data);
        });
    });

Solution

  • Solution:

    If you want to return from server pdf, you must use binary param for res.end.

    generateDoc().then(function(data) {
        res.set({
            'Content-Type': 'application/pdf',
            'Content-Length': data.length
        });
        res.end(data, 'binary');
    }).fail(function (error) {
        res.end(500, "Some error");
    });