Tell us what’s happening:
Describe your issue in detail here.
The code works fine on my computer but on replit once the json data is sent it displays it on the webpage however it quickly redirects to the index.html page.
And when I submit it the error, I’m getting is this
"[SyntaxError: Unexpected token ‘<’, "<!DOCTYPE “… is not valid JSON]” Your project link(s)
Good day. First thing I noticed your route is wrong instead of
app.post('/', upload.single('upfile'), function (req, res, next) {
const data = {
"name": req.file.originalname,
"size": req.file.size,
"type": req.file.mimetype
};
res.json(data); // Send JSON response
console.log(data);
});
//You should do the following route:
app.post('/api/fileanalize', upload.single('upfile'), function (req, res, next) {
const data = {
"name": req.file.originalname,
"size": req.file.size,
"type": req.file.mimetype
};
res.json(data); // Send JSON response
console.log(data);
});
The route is found in the views folder in index.js in for tag action=“/api/fileanalyse”
If you view instructions on multer NPM How to upload form with multer
Please read the Usage part about the following:
Profile is gotten from action attribute in form. Then added to route. Enctype is added to multer middle ware function in post method.
<form action="/profile" method="post" enctype="multipart/form-data">
<input type="file" name="avatar" />
</form>
app.post('/profile', upload.single('avatar'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})