0

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

0

1 Answer 1

1

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());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.