Express Error:TypeError:Router.use()需要中间件函数但得到一个对象

时间:2016-08-29 11:45:01

标签: javascript node.js express

我正在创建一个上传/下载应用,我不知道为什么它只有我的app.js文件出现此问题。我在互联网上搜索了所有其他答案,但仍然没有解决问题。 这是下面的代码:

    var express = require('express');
    var path = require('path');  // add path to set and get
    var fs = require('fs');  // add filesystem
    var connect = require('connect');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');
    var connect = require('connect');
    var http = require('http');
    var routes = require('./routes/index');
    var users = require('./routes/users');
    //var upload = require('./routes/upload');
    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(path.join(__dirname, 'public', 'favicon.ico')));
    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));
    app.use(express.static('public'));
    app.use(express.static('files'));
    app.use('/', routes);
    app.use('/users', users);
    //app.use('/upload',upload);
    // 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 handlers
    // development error handler
   // will print stacktrace
   if (app.get('env') === 'development') {
   app.use(function(err, req, res, next) {
   res.status(err.status || 500);
   res.render('error', {
   message: err.message,
   error: err
   });
   });
   }
   // production error handler
   // no stacktraces leaked to user
   app.use(function(err, req, res, next) {
   res.status(err.status || 500);
   res.render('error', {
   message: err.message,
   error: {}
   });
   });
   module.exports = app;

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

检入所有文件 确保你不要错过export

module.exports = router;

答案 1 :(得分:0)

Router.use()始终需要使用中间件功能作为参数。假设您从服务器下载某些文件的路线,它应该与此类似。

var Router = express.Router();


function downloadFiles(request,response) {
   // some code to dowload files
}

// The above function which takes request and response
// should be passed to Router.use()

Router.use(downloadFiles);

当您出现该错误时,它意味着您传递给Router.use()的内容不是像downloadFiles这样的中间件函数。