javascript - 如何在 ExpressJS 中使用 Q Promise?

标签 javascript node.js express node.js-connect

我需要生成 pdf 文档。关于 pdf 的所有魔力都在 generateDoc 函数中,该函数返回带有 Buffer 数据作为参数的 Promise。但 Express 不向客户端发送数据,仅向客户端发送 header 。我做错了什么?

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

最佳答案

解决方案:

如果要从服务器返回 pdf,则必须对 res.end 使用 binary 参数。

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");
});

关于javascript - 如何在 ExpressJS 中使用 Q Promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19658350/

相关文章:

javascript - Jquery array.push() 不工作

javascript - 如何在我的 html 页面中使用 javascript 函数重定向到分区(<div>)

apache - 在 Ec2 上使用 Apache Proxy 部署 NodeJS(错误?)并记录日志

javascript - Node : Testing with sinon and async/await

javascript - 在 Express 中从一条路线重定向到另一条路线

javascript - Symfony 2.7、Twig 和 CKEditor -- 如何安全地允许某些 HTML 输出?

javascript - React js 和 MUI 中的三 Angular 形 slider

node.js - Mongoose 模型测试需要模型

node.js - 在异步完成之前将 'Received post' 发送回请求者(NodeJS、ExpressJS)

javascript - 准备部署express js应用程序,我应该担心哪些安全问题?