Javascript Node.js使用officegen创建和下载word文档

Javascript Node.js使用officegen创建和下载word文档,javascript,node.js,ms-word,Javascript,Node.js,Ms Word,我想使用officegen npm模块创建一个word(docx)文件并下载它。在过去,我使用tempfile模块创建了一个用于下载的临时路径。以下是我为word文档下载编写的代码: var tempfile = require('tempfile'); var officegen = require('officegen'); var docx = officegen('docx'); router.post('/docx', function(req, res){ var temp

我想使用officegen npm模块创建一个word(docx)文件并下载它。在过去,我使用tempfile模块创建了一个用于下载的临时路径。以下是我为word文档下载编写的代码:

var tempfile = require('tempfile');
var officegen = require('officegen');
var docx = officegen('docx');

router.post('/docx', function(req, res){
    var tempFilePath = tempfile('.docx');
    docx.setDocSubject ( 'testDoc Subject' );
    docx.setDocKeywords ( 'keywords' );
    docx.setDescription ( 'test description' );

    var pObj = docx.createP({align: 'center'});
    pObj.addText('Policy Data', {bold: true, underline: true});

    docx.on('finalize', function(written) {
        console.log('Finish to create Word file.\nTotal bytes created: ' + written + '\n');
    });
    docx.on('error', function(err) {
        console.log(err);
    });

    docx.generate(tempFilePath).then(function() {
        res.sendFile(tempFilePath, function(err){
            if(err) {
                console.log('---------- error downloading file: ' + err);
            }
        });
    });
});
然而,它给了我一个错误的说法

TypeError:dest.on不是函数


创建的总字节数也显示为0。我做错了什么

如果您使用的是最新的officegen,那么out应该是文件流,而不仅仅是文件。请参阅手册。您还可以直接生成响应,如
docx.generate(res)@Andrey谢谢。我做了doc.generate(res),它成功了。谢谢你的帮助
router.post('/docx', function(req, res){
    var tempFilePath = tempfile('.docx');
    docx.setDocSubject ( 'testDoc Subject' );
    docx.setDocKeywords ( 'keywords' );
    docx.setDescription ( 'test description' );

    var pObj = docx.createP({align: 'center'});
    pObj.addText('Policy Data', {bold: true, underline: true});

    docx.on('finalize', function(written) {
        console.log('Finish to create Word file.\nTotal bytes created: ' + written + '\n');
    });
    docx.on('error', function(err) {
        console.log(err);
    });

   res.writeHead ( 200, {
    "Content-Type": "application/vnd.openxmlformats-officedocument.documentml.document",
    'Content-disposition': 'attachment; filename=testdoc.docx'
    });
    docx.generate(res);
});