Asgeir
Asgeir

Reputation: 667

How can I fix "app.use() requires a middleware function"

So according to the error message at the bottom, the problem occurs on line 11, which is where I give multer some parameters. As it looks like there shouldn't be any problems, I initially thought multer wasn't installed properly, but all the multer files (I believe) are in place under node_modules. Now I'm clueless as to what the problem might me.

var express = require('express');
var app = express();
var fs = require('fs');

var multer = require('multer');
var bodyParser = require('body-parser');


app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/tmp/'  }));

app.get('/index.html', function (req, res) {
   res.sendFile( __dirname + "/" + "index.html" );
})

app.post('/file_upload', function (req, res) {
    console.log(req.files.file.name);
   console.log(req.files.file.path);
   console.log(req.files.file.type);
   var file = __dirname + "/" + req.files.file.name;

   fs.readFile( req.files.file.path, function (err, data) {
      fs.writeFile(file, data, function (err) {
         if( err ) {
            console.log( err );
            } else {
               response = {
                  message:'File uploaded successfully',
                  filename:req.files.file.name
               };
            }

         console.log( response );
         res.end( JSON.stringify( response ) );
      });
   });    
})

var server = app.listen(8080, function () {
   var host = server.address().address
   var port = server.address().port

   console.log("Post app listening to://%s:%s", host, port)
})
TypeError: app.use() requires a middleware function
    at EventEmitter.use (/home/asgeir/nodejs/first_test_app/node_modules/express/lib/application.js:210:11)
    at Object.<anonymous> (/home/asgeir/nodejs/first_test_app/server.js:11:5)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
/home/asgeir/nodejs/first_test_app/node_modules/express/lib/application.js:210
    throw new TypeError('app.use() requires a middleware function')
    ^

Upvotes: 0

Views: 795

Answers (2)

Rahi
Rahi

Reputation: 324

Try

using app.use(multer({dest: '/tmp/'}).any());

Please refer to this question https://stackoverflow.com/a/36020740/2558788

Upvotes: 3

BENARD Patrick
BENARD Patrick

Reputation: 30975

It seems that you've got a problem with multer.

In reading the doc, you don't implement correctly the integration.

Look at this extract of doc: https://github.com/expressjs/multer#usage

You've to customize deplendling of your needs...

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

var app = express()

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

var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
  // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
  //
  // e.g.
  //  req.files['avatar'][0] -> File
  //  req.files['gallery'] -> Array
  //
  // req.body will contain the text fields, if there were any
})

Upvotes: 1

Related Questions