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);
});