typeerror: app.use() requires middleware function

允我心安 提交于 2019-11-28 19:10:44

It worked in this way. delete this line,

app.use(multer({ dest: './uploads' }));

and use it as,

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

I'm too following the same course

Change:

//Handle File Uploads
app.use(multer({
  dest: './uploads'
}));

For something like:

app.use(multer({dest:'./uploads/'}).single('singleInputFileName'));

or

app.use(multer({dest:'./uploads/'}).array('multiInputFileName'));

And be aware to have something like:

<form action="/postPhotos" enctype="multipart/form-data">
    <input type="file" name="singleInputFileName">
    <input type="submit" value="Upload photo">
</form>

for the first case, or:

<form action="/postPhotos" enctype="multipart/form-data">
    <input type="file" name="multiInputFileName" multiple>
    <input type="submit" value="Upload photo">
</form>

For the second one, in your html.

AbdulMomen عبدالمؤمن

This worked with me:

app.use(multer({
  dest: path.join(__dirname, 'public/upload/temp')
}).any());

I was also doing that course & ran into the same problem. I resolved the error by following the Multer usage instructions from their github: https://github.com/expressjs/multer

Here is the code from their README:

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

& here is the code I used to overcome the error while doing that tutorial:

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

Hope it helps :)

you can simply use :

app.set(multer({dest: "./uploads"}));

it works for me too.

change only this line

app.use(multer({dest:'./uploads'}));

to

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

Replacing app.use(multer({dest:'./uploads'}));

to

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

worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!