Иво Стратев
Иво Стратев

Reputation: 415

NodeJs Multer error

I'm getting the follow error while trying to retrieve uploaded files. I've searched the net and all I found is that people are injecting the result from multer() as midleware but all I got is errors ...

/node_modules/express/lib/router/index.js:458
  throw new TypeError('Router.use() requires middleware function but got a
        ^
TypeError: Router.use() requires middleware function but got a Object
at Function.use (/node_modules/express/lib/router/index.js:458:13)
at EventEmitter.<anonymous> (/node_modules/express/lib/application.js:219:21)
at Array.forEach (native)
at EventEmitter.use (/node_modules/express/lib/application.js:216:7)
at Object.<anonymous> (/app.js:40:5)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)

Here is the code of that part of the app

var express = require('express');
var multer = require('multer');
var mul = multer({limits : {fileSize : 1000000, files : 10}});
var app = express();
var port = 3000;
var server = require('http');
app.use('/files', mul, files);
function files (req, res) {
  console.log(req.files, req.file);
}
server = server.createServer(app);
server.listen(process.env.PORT || port);

Upvotes: 2

Views: 1210

Answers (2)

Иво Стратев
Иво Стратев

Reputation: 415

Here is the code that works, thanks to @shershen

//other stuff    
var mul = multer({limits : {fileSize : 1000000, files : 10}});
app.use('/files', mul.any(), files);
function files (req, res) {
console.log(req.files, req.file);
}
//other stuff

Upvotes: 2

shershen
shershen

Reputation: 10003

Code you have for configuring routes and multer seems a bit wrong. Thats's how it's described to be here. Second parameter(s) in app.use should be function or functions app.use docs while mul in your code is an Object, result of your configuration factory call earlier here : var mul = multer({limits : {fileSize : 1000000, files : 10}});

//other stuff    
var mul = multer({limits : {fileSize : 1000000, files : 10}});

app.use('/files', [mul, files]);

function files (req, res) {
  console.log(req.files, req.file);
}
//other stuff

UPD

did you try like this, according to docs on module page?

var mul = multer({ dest: 'your_uploads_path/' });
app.use('/files', mul.array(), files); //or other option 

other options are here: https://www.npmjs.com/package/multer#multer-opts

UPD2

According to docs multer indeed returns object, that should be consumed by busboy, so you need it to be required and configured as well - https://github.com/mscdex/busboy#busboy-methods

Upvotes: 2

Related Questions