I have created a signup form in HTML where the data collected is stored in excel using Javascript and a couple of libraries like exceljs, prohairesis, express, morgan, body-parser, etc. This is done on localhost, so to start the program, I use ‘npm start’ in command prompt.
However in any normal website, the user needs to be able to navigate from the home page to the signup webpage. But because my home page is just an HTML file and my signup page is a localhost server, I am unable to link the two.
I’m looking for a way to probably achieve the same task of collecting the data from the HTML form into an excel workbook without using a localhost server or a port.
Or if there is any other way of linking the home page and the signup page I would love to try it out. Just to be clear, navigating to and fro is my objective.
Here is the javascript code that collects data:
//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 fs = require("fs");
const app = express();
const port = process.env.PORT || 8081;
let info = [];
app
.use(express.static("public"))
.use(morgan("dev"))
.use(bodyParser.urlencoded({ extended: false }))
.use(bodyParser.json())
.post("/api/user", async (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 < 8)
{
res.json("**Password must contain at least 8 characters**")
}
if (user_data.pwd != user_data.cpwd)
{
console.log("**Password does not match**");
res.json("**Password does not match**");
break;
}
if (user_data.pwd == user_data.cpwd)
{
res.json("Thank you for signing up with us!");
info.push(user_li);
console.log(info);
//append row to excel worksheet
const workbook = new Excel.Workbook();
// for safety
try {
// check if `Login-Db.xlsx` file exists
if (fs.existsSync("Login-Db.xlsx")) {
// load existing workbook
workbook.xlsx.readFile("Login-Db.xlsx").then((workbook) => {
// get worksheet
const worksheet = workbook.getWorksheet("Main Db");
// append rows to worksheet
worksheet.addRows(info);
// save workbook
workbook.xlsx.writeFile("Login-Db.xlsx").then((err) => {
if (!err) {
console.log("Row added to excel file");
return;
}
// if error, print it
console.log(err);
});
});
} else {
// create new worksheet
const worksheet = workbook.addWorksheet("Main Db");
// add new rows to worksheet
worksheet.addRows(info);
// save workbook
workbook.xlsx.writeFile("Login-Db.xlsx").then((err) => {
if (!err) {
console.log("Row added to excel file");
return;
}
// if error, print it
console.log(err);
});
}
} catch (error) {
console.log(error);
}
break;
}
}
})
.listen(port, () => console.log(`Server listening on port ${port}`));
I am a beginner in web development and I’m quite stuck at this point….Hoping for assistance
Thanks in advance 🙂