So i have a nodeJS app using multer to upload excel file and read it , but there is no saved file . The app is woking fine on localhost , the trouble is somewhere in configuring Netlifly
Here is the project https://quiz-gvv.netlify.app
Here is Github https://github.com/GerganaVVladimirova/Quiz.git
There is another problem , after uploading the file I should be redirected to quiz.html , but it is not happening on Netlifly , only on localhost
here is my api.js
const express = require("express");
const multer = require("multer");
const { promisify } = require("util");
const app = express();
const port = 3000;
const serverless = require('serverless-http');
const router = express.Router();
const fs = require("fs");
const xlsx = require("xlsx");
const { ok } = require("assert");
const unlinkAsync = promisify(fs.unlink);
// Serve static files from the "public" directory
app.use(express.static("../dist"));
let excelFilePath = '';
let exam = 0;
let questions = [];
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
cb(null, Date.now() + '-' + file.originalname);
}
});
const upload = multer({ storage: storage }).single("file");
router.post('/upload',upload, async (req, res) => {
exam = req.body.picker;
excelFilePath = req.file.path; // Save the path of the uploaded file
const workbook = xlsx.readFile(excelFilePath);
let sheetNumber = Number(exam)-1;
const sheetName = workbook.SheetNames[sheetNumber];
const sheet = workbook.Sheets[sheetName];
// Parse the sheet data into JSON format
const excelData = xlsx.utils.sheet_to_json(sheet);
if (excelData) {
questions.push(objQuestion);
}
}
await unlinkAsync(req.file.path);
res.redirect('quiz.html');
});
router.get('/questions',(req,res)=>{
// console.log(questions)
if(questions){
res.json({questions});
}
});
app.use('/.netlify/functions/api', router);
module.exports.handler = serverless(app);
// app.listen(port, () => {
// console.log(`Server listening at http://localhost:${port}`);
// });
Here is the code from postman
<body>
<pre>Error: ENOENT: no such file or directory, open 'uploads/1715433402132-default.xlsx'</pre>
</body>
Gergana Vladimirova is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.