multer configuration with app.use returns TypeError

跟風遠走 提交于 2019-11-30 06:56:08

Problem is that multer({dest:'./uploads/'}) return object, not middleware function. And if we look at the code module that we will see that there are three middlware functions:

multer({dest:'./uploads/'}).single(...)
multer({dest:'./uploads/'}).array(...)
multer({dest:'./uploads/'}).fields(...)

Try one of them.

Credit goes to @stdob-- for guiding me in the right direction. The multer documentation I was looking at seems to have been updated while I was working and therefore I was using the code incorrectly.

Multer should be used with its middleware functions. Consequently the correct way to use this package is as follows:

var multer = require('multer');

var upload = multer({dest:'uploads/'});

var cpUpload = upload.single('postImg');

router.post('/createpost', cpUpload, function(req,res){

  console.log(req.file);
  res.redirect('/');

}

This code snippet will create a post route for '/createpost' and will upload a single document (by the form name 'postImg') to /uploads in the server. req.file contains the JSON with the file information (such as the file path). Keep in mind that this will not automatically get rid of the uploaded file.

More information can be found here: https://www.npmjs.com/package/multer

Hamid Ali

this will do

app.use(multer({dest:'./public/images/uploads'}).any());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!