I'm trying to integrate express-fileupload
to upload and image to my server. I have followed the example provided in this link, express-fileupload Examples but It doesn't work. Below I have included each file involved with only the relevant lines of code.
index.ejs: Relevant lines
<form method="post" action="/images" enctype="multipart/formdata">
<div class="panel-body form-horizontal">
<div class="form-group col-md-12">
<label class="col-sm-2 control-label" for="file">Browse:</label>
<div class="col-md-10">
<input class="form-control" type="file" name="file" id="file">
</div>
</div>
</div>
</form>
server.js
const express = require('express');
const config = require('./server/configure');
let app = new express();
app.set('port', process.env.PORT || 3300);
app = config(app);
app.listen(app.get('port'), () => {
console.log(`Server up: http://localhost:${app.get('port')}`);
});
configure.js
const express = require('express');
const path = require('path');
const expressFileUpload = require('express-fileupload');
const routes = require('./routes');
module.exports = (app) => {
// applying expressFileUpload middleware
app.use(expressFileUpload());
routes(app);
app.use('/public/', express.static(path.join(__dirname, '../public')));
return app;
};
routes.js
const image = require('../controllers/image');
module.exports = (app) => {
app.post('/images', image.create);
};
image.js
module.exports = {
create(req, res) {
let image = req.files.file;
}
}
Error
I get an error at the following line let image = req.files.file;
which states,
TypeError: Cannot read property 'file' of undefined
Why is file undefined and how do I correctly upload an image using express-fileunload?