Multer: Can Multer work without being a middleware?

Created on 14 Nov 2016  路  5Comments  路  Source: expressjs/multer

Hello guys!

Has multer a function to upload the file only based on the request? I mean being a simple function without being a middleware function using next.

I would like to know if there is something like that to achieve this: graphql/express-graphql#123.

Thanks!

Most helpful comment

I wanted to know the same thing and tried what @LinusU recommended and got the same error.

I went back to look at the usage in the readme which shows:

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

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files
  // req.body will contain the text fields, if there were any
})

So you actually need to call

upload.single('avatar')(req, {}, function (err) {
  if (err) throw err

  // req.file, req.files...
})

All 5 comments

You can call the middleware function with just an empty object as res:

upload(req, {}, function (err) {
  if (err) throw err

  // req.file, req.files...
})

That does not work for me. What is the upload function for you? I have this: const upload = multer({ storage });, and the terminal throws an error saying "TypeError: upload is not a function".

On which line does "TypeError: upload is not a function" happen? Can you post your code?

I wanted to know the same thing and tried what @LinusU recommended and got the same error.

I went back to look at the usage in the readme which shows:

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

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files
  // req.body will contain the text fields, if there were any
})

So you actually need to call

upload.single('avatar')(req, {}, function (err) {
  if (err) throw err

  // req.file, req.files...
})

Closing as resolved 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nickretallack picture nickretallack  路  4Comments

ChristianRich picture ChristianRich  路  4Comments

Paul-Morris picture Paul-Morris  路  3Comments

josephstgh picture josephstgh  路  3Comments

bpetty-formfast picture bpetty-formfast  路  3Comments