Adam
Adam

Reputation: 897

Express JS 4.x Multer Basic Upload

I'm trying to upload an image in Express JS using the Multer middleware, but running into several issues when trying to set up the absolute simplest use case.

The only error I'm receiving is: 'POST /upload 500', and my POST callback is never entered.. so I'm not even sure how to debug this case. My code is as follows:

app.js:

var express = require('express');
var multer = require('multer');
var upload = multer({ dest: './public/photos/'}); // valid dir

app.post('/upload', upload.single('photo'), function(req, res, next){
  // This callback is never reached. 500 error.
  console.log(req.body);
  console.log(req.files);
});

upload.ejs:

<form method='post' enctype='multipart/form-data'>
    <p><input type='text', name='photo[name]', placeholder='Name'/></p>
    <p><input type='file', name='photo[image]'/></p>
    <p><input type='submit', value='Upload'/></p>
</form>

I can't pick out any difference between this and the current npm Multer setup documentation. More than just 'how-to' fix this, I'd really like to know why this isn't working, and what's happening under the hood that's causing it to fail.

All help welcome. THANK YOU!!!

Upvotes: 1

Views: 1626

Answers (1)

Adam
Adam

Reputation: 897

Solved! For one, my input names were not matching: upload.single('photo[image]').

That's what was causing the 500 error. I had a couple issues after this that were caused by using an outdated req.file packet and were easily solved by referencing the up-to-date multer documentation.

Upvotes: 1

Related Questions