Back End Development and APIs Projects - File Metadata Microservice

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)

solution: boilerplate-project-filemetadata (3) - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - File Metadata Microservice

Link to the challenge:

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
})
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.