Igor Kochetkov
Igor Kochetkov

Reputation: 5

generate excel via nodeJS in Azure Functions

I have nodejs app with expressJS and excel4node library, which is running on local machine. I'm sending REST messages to this server and it returns me excel binary file.

I want to move it Azure Functions, but facing with issue. Even simple app (took from example) is not running there. Maybe someone have suggestions how to solve this?

const createHandler =  require('azure-function-express').createHandler;
const express = require('express');
const xl = require('excel4node')
// Create express app as usual
const app = express();
app.post('/api/hello-world', (req, res) => {
    var wb = new xl.Workbook();
    var ws = wb.addWorksheet('S');
    ws.cell(1, 1).string('A');
    wb.write(`FileName.xlsx`, res);
});
// Binds the express app to an Azure Function handler
module.exports = createHandler(app);

and this is the error what I'm seeing :

Microsoft.AspNetCore.Server.Kestrel.Core: Response Content-Length mismatch: too many bytes written (3790 of 3569).

Does someone know how to solve it, or maybe have an example of generating excel in Azure Functions via NodeJS

Upvotes: 0

Views: 586

Answers (1)

René
René

Reputation: 66

Just in case anyone else stubles upon this looking for the answer (like I did). This works for me:

var xl = require('excel4node');

const tryCreate = async (obj) => {
    let wb = new xl.Workbook();

    const buffer = await wb.writeToBuffer();
    return {
        setEncoding: 'binary',
        // status: 200, /* Defaults to 200 */
        body: buffer
    };
}

module.exports = async function (context, req) {
    try {
        context.res = await tryCreate(req.body);
    } catch (error) {
        context.log.error(error, new Date().toISOString());
    }
}

Upvotes: 1

Related Questions