multer configuration with app.use returns TypeErro

2019-02-05 01:09发布

I'm trying to configure multer in my app.js file (using node.js/express) in order to allow users to upload images. I have the following code in app.js:

//various require statements for passport, cookie parser, etc..

var multer = require('multer');

var app = express();

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

When I try to run the app I get TypeError: app.use() requires middleware functions

I understand that this questions may require some more context with regards to my app so please let me know if additional information is needed.

Thank you

EDIT: More code from app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var session = require('express-session');
//var fs = require('fs');
var multer = require('multer');

//Mongo Database
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/test-api');

//Instagram-API
var api = require('instagram-node').instagram();

//Cookie Manager
var cookieParser = require('cookie-parser');

//Grid
//var Grid = require('gridfs-stream');

//Passport
var passport = require('passport');
var InstagramStrategy = require('passport-instagram').Strategy;

var routes = require('./routes/index');
//var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(multer({dest:'./uploads/'}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(methodOverride());
app.use(session({secret: 'keyboard cat', resave: false, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));


// Make our db accessible to our router
app.use(function(req,res,next){
    req.db = db;
    next();
});

app.use('/', routes);

3条回答
做个烂人
2楼-- · 2019-02-05 01:47

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-05 02:08

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

查看更多
等我变得足够好
4楼-- · 2019-02-05 02:09

this will do

app.use(multer({dest:'./public/images/uploads'}).any());
查看更多
登录 后发表回答