I am new to programming
INDEX.JS
const express = require("express")
const path = require("path")
const multer = require("multer")
const app = express()
const maxSize = 1 * 1000 * 1000;
app.use(express.static(path.join(__dirname, 'public')))
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads")
},
filename: function (req, file, cb) {
cb(null, file.fieldname + "-" + Date.now()+".pdf")
}
})
var upload = multer({storage: storage, limits: { fileSize: maxSize }}).array('fileUpload', 2);
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get("/uploads/:filename", (req, res) => {
let filename = req.params.filename;
res.sendFile(__dirname + "/uploads/" + filename)
});
app.post("/upload",function (req, res, next) {
upload(req,res,function(err) {
if(err) {
res.send(err)
}
else {
let str = `<p>Success, pdf uploaded!</p>
<p><a href="${req.file.path}">View File</a></p>`;
res.send(str)
}
})
})
app.listen(9000,function(error) {
if(error) throw error
console.log("Server created Successfully on PORT 9000")
console.log("http://localhost:9000")
})
INDEX.HTML
<!DOCTYPE html>
<html>
<head>
<link href="main.css" rel="stylesheet">
</head>
<body>
<form action="/upload" enctype="multipart/form-data" method="POST">
<input type="file" name="fileUpload" id="fileUpload" hidden="hidden" multiple /> <br>
<button type="button" id="button1">UPLOAD FILES</button>
<input type="submit" value="submit" />
</form>
<script src="main.js"></script>
</body>
</html>
MAIN.JS
const customBtn = document.getElementById("button1");
const fileUpload = document.getElementById("fileUpload")
customBtn.addEventListener("click", function() {
fileUpload.click();
});
fileUpload.addEventListener("change", function() {
if (fileUpload.value) {
customBtn.innerHTML = fileUpload.value.match(
/[\/\\]([\w\d\s\.\-\(\)]+)$/
)[1];
} else {
customBtn.innerHTML = "No file chosen, yet.";
}
});
When I run INDEX.js i get the following error Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path') The pdfs get uploaded to my UPLOADS folder but I still get the error Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path')