Express JS Error hrow new TypeError('app.use()需要中间件函数');

时间:2017-07-31 08:07:29

标签: javascript node.js express

我的代码在这里有什么问题是我的app.js文件,当我点击npm start时,它会给出如下错误:

TypeError('app.use() requires middleware functions'); 

我使用了以下代码。

          var express = require('express');
          var path = require('path');
          var favicon = require('serve-favicon');
          var logger = require('morgan');
          var cookieParser = require('cookie-parser');
          var bodyParser = require('body-parser');
          var session = require('express-session');
          var expressValidator = require('express-validator');
          var passport = require('passport');
          var LocalStrategy = require('passport-local').Strategy;
          var multer = require('multer');
          var flash = require('connect-flash');
          var mongo = require('mongodb');
          var mongoose = require('mongoose');
          var db = mongoose.connection;


          var index = 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', 'ejs');

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


          // uncomment after placing your favicon in /public
          //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
          app.use(logger('dev'));
          app.use(bodyParser.json());
          app.use(bodyParser.urlencoded({
            extended: false
          }));
          app.use(session({
            secret: 'secret',
            resave: true,
            saveUninitialized: true
          }));
          app.use(passport.initialize());
          app.use(passport.session());
          app.use(expressValidator({
            errorFormatter: function(param, msg, value) {
              var namespace = param.split('.'),
                root = namespace.shift(),
                formParam = root;

              while (namespace.length) {
                formParam += '[' + namespace.shift() + ']';
              }
              return {
                param: formParam,
                msg: msg,
                value: value
              };
            }
          }));
          app.use(cookieParser());
          app.use(express.static(path.join(__dirname, 'public')));
          app.use(flash());
          app.use(function(req, res, next) {
            res.locals.messages = require('express-messages')(req, res);
            next();
          });


          app.use('/', index);
          app.use('/users', users);

          // catch 404 and forward to error handler
          app.use(function(req, res, next) {
            var err = new Error('Not Found');
            err.status = 404;
            next(err);
          });

          // error handler
          app.use(function(err, req, res, next) {
            // set locals, only providing error in development
            res.locals.message = err.message;
            res.locals.error = req.app.get('env') === 'development' ? err : {};

            // render the error page
            res.status(err.status || 500);
            res.render('error');
          });

          module.exports = app;

在控制台上我得到了以下输出,

TypeError: app.use() requires middleware functions at Function.use (C:\Users\Rohit Jain\Desktop\nodjs\userloginsystem\node_modules\express\lib\application.js:210:11) at Object. (C:\Users\Rohit Jain\Desktop\nodjs\userloginsystem\app.js:27:5) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.require (module.js:498:17) at require (internal/module.js:20:19) at Object. (C:\Users\Rohit Jain\Desktop\nodjs\userloginsystem\bin\www:7:11) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) at run (bootstrap_node.js:425:7) at startup (bootstrap_node.js:146:9) at bootstrap_node.js:540:3

请帮我解决此问题。

1 个答案:

答案 0 :(得分:0)

您使用的multer模块没有正确的详细信息,请查看更多文档here。 问题是multer({dest:'./uploads/'})返回对象,而不是中间件函数。

27号

行使用以下行
app.use(multer({dest:'./uploads'}).any());

<强> .ANY()

接受通过网络传输的所有文件。一系列文件将存储在req.files中。

以下其他选项如下:

  1. 。单(字段名)
  2. .array(fieldname [,maxCount])
  3. 点域(字段)
  4. .none()
  5. .ANY()
  6. 有关详细信息,请查看同一问题here

    希望这会对你有所帮助。