I need a simple help about how to generate Excel and then export it on click of API url in serverless offline.It should show a download option for that file. When I am creating a file in normal node with express frework.It is generating correct Excel file and I am able to download that file easily. But when I am using the same code in serverless offline I am getting a corrupted .xlsx file on download. I am new to serverless offline and know only basics of creating and using lambda functions offline. Please help me for this task.
**app.js:**
var Excel = require('exceljs');
var app = express();
var nodeExcel = require('excel-export');
const serverless = require('serverless-http')
app.get("/click", async (req, res) => {
var workbook = new Excel.Workbook();
var worksheet = workbook.addWorksheet('My Sheet');
worksheet.columns = [
{ header: 'Id', key: 'id', width: 10 },
{ header: 'Name', key: 'name', width: 32 },
{ header: 'D.O.B.', key: 'DOB', width: 10 }
];
worksheet.addRow({ id: 1, name: 'John Doe', dob: new Date(1970, 1, 1) });
worksheet.addRow({ id: 2, name: 'Jane Doe', dob: new Date(1965, 1, 7) });
await workbook.xlsx.writeFile('./temp.xlsx')
var fileName = 'temp.xlsx';
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
await workbook.xlsx.write(res);
})
module.exports.handler = serverless(app);
**Serverless.yml:**
plugins:
- serverless-offline
provider:
name: aws
runtime: nodejs10.x
custom:
serverless-offline:
host: '0.0.0.0'
functions:
app:
handler: route/app.handler
events:
- http:
path: /click
method: get