I have created an HTML form that takes in user data and using exceljs and a few other libraries I store this data into an excel spreadsheet. This part of my code works perfectly fine. However, when I refresh the server, the data stored in the excel file previously gets deleted.
I believe the reason is that I could not save the excel workbook....but when I searched up for some code to achieve this and executed it...the code didn't work.
Here is the code that collects user data into the excel worksheet:
//importing necessary libraries
const express = require("express");
const morgan = require("morgan");
const Prohairesis = require("prohairesis");
const bodyParser = require("body-parser");
const Excel = require("exceljs");
const app = express();
const port = process.env.PORT || 8081;
var info = []
app
.use(express.static('public'))
.use(morgan('dev'))
.use(bodyParser.urlencoded({extended: false}))
.use(bodyParser.json())
.post('/api/user', (req, res) => {
res.json(req.body);
//collecting user data into a javascript string
const user = req.body;
const ud = JSON.stringify(user);
const user_data = JSON.parse(ud);
console.log(user_data);
const user_li = [user_data.first, user_data.email, user_data.stdid, user_data.pwd, user_data.cpwd];
console.log(user_li);
//some simple validation
for (i in user_li)
{
if (user_data.pwd != user_data.cpwd)
{
console.log("**Password does not match**");
break;
}
if (user_data.pwd == user_data.cpwd)
{
info.push(user_li);
console.log(info);
//append row to excel worksheet
const workbook = new Excel.Workbook();
const worksheet = workbook.addWorksheet('Main Db');
worksheet.addRows(info);
workbook.xlsx.writeFile("Login-Db.xlsx")
console.log('Row has been appended');
break;
}
}
})
.listen(port, () => console.log('Server listening on port ${port}'));