Metadata Microservice- Can anyone tell me what I'm doing wrong?

Metadata Microservice- Can anyone tell me what I'm doing wrong?
0

#1

I’ve been stuck on this for a while now, because req.file has been undefined every time I’ve tried running the code. There’s never an error in the logs so it’s not a bug, but that’s making this difficult to figure out what it is I’m doing wrong. Can anyone help?

<!DOCTYPE html>
<html>
  <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
  <script src="/client.js"></script>
  <link rel="stylesheet" href="/style.css">

  <head>
    <title>FreeCodeCamp MetaData API Project</title>
  </head>
  <body>
  
    <div id="main">
      <h1>
        FreeCodeCamp MetaData API
      </h1>
      <p>
        Upload a document here to get a JSON object to display the number of bytes within the document.
      </p>
      <form method="post" enc-type="multipart/form-data" action="/api/upload">
        <input type="file" name="file">
        <input type="submit" value="Upload!">
      </form>
    </div> 
  </body>
</html>
const express = require('express');
const app = express();

const multer = require('multer');
const storage = multer.memoryStorage();
const upload = multer({storage: storage});

app.use(express.static('public'));

app.get("/", function (request, response) {
  response.sendFile(__dirname + '/views/index.html');
});

app.post('/api/upload', upload.single('file'), function(req,res, next){
  if(!req.file){
    res.end('Check Logs');
  }else if(req.file)  {
    res.json({size: req.file.size})
  }
  console.log(req.file);
});

var listener = app.listen(process.env.PORT, function () {
  console.log('App launched and listening on ' + listener.address().port);
});

#2

It should enctype instead of enc-type.


#3

It’s still returning undefined.

EDIT: After forcing my browser to clear the cache, it finally works! Thank you.

2nd Edit: I’m really glad I’m going through all of these challenges, and keeping all of the code I write. I’ll never have to spend days tearing my hair out to find the issue again, and I’ll always have this kinda code to look back on and reference instead of going through StackOverflow and other people’s code.