2015-10-09 93 views
0

我正在開發Express Js中的應用程序。當我嘗試運行應用程序我得到這個錯誤:Express Js:Router.use()需要中間件功能

enter image description here

app.js文件是這樣的:

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 routes = require('./routes/index'); 
var users = require('./routes/users'); 
var app = express(); 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'jade'); 
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('/', routes); 
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); 
}); 
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 
    }); 
    }); 
} 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
    message: err.message, 
    error: {} 
    }); 
}); 
var server = app.listen(3000, function() { 
var host = server.address().address; 
var port = server.address().port; 
console.log('Example app listening at http://%s:%s', host, port); 
}); 
module.exports = router; 

index.js是這樣的:

var express = require('express'); 
var router = express.Router(); 

/* GET home page. */ 
router.get('/', function (req, res, next) { 
    res.render('index', {title: 'Polls'}); 
}); 

router.get("/list", function (req, res, next) { 
    Poll.find({}, 'question', function (error, polls) { 
     res.json(polls); 
    }); 
}); 

router.get("/poll", function (req, res, next) { 
    var pollId = req.params.id; 

    // Find the poll by its ID, use lean as we won't be changing it 
    Poll.findById(pollId, '', {lean: true}, function (err, poll) { 
     if (poll) { 
      var userVoted = false, 
        userChoice, 
        totalVotes = 0; 

      // Loop through poll choices to determine if user has voted 
      // on this poll, and if so, what they selected 
      for (c in poll.choices) { 
       var choice = poll.choices[c]; 

       for (v in choice.votes) { 
        var vote = choice.votes[v]; 
        totalVotes++; 

        if (vote.ip === (req.header('x-forwarded-for') || req.ip)) { 
         userVoted = true; 
         userChoice = {_id: choice._id, text: choice.text}; 
        } 
       } 
      } 

      // Attach info about user's past voting on this poll 
      poll.userVoted = userVoted; 
      poll.userChoice = userChoice; 

      poll.totalVotes = totalVotes; 

      res.json(poll); 
     } else { 
      res.json({error: true}); 
     } 
    }); 
}); 

router.get("/create", function (req, res, next) { 
    var reqBody = req.body, 
      // Filter out choices with empty text 
      choices = reqBody.choices.filter(function (v) { 
       return v.text != ''; 
      }), 
      // Build up poll object to save 
      pollObj = {question: reqBody.question, choices: choices}; 

    // Create poll model from built up poll object 
    var poll = new Poll(pollObj); 

    // Save poll to DB 
    poll.save(function (err, doc) { 
     if (err || !doc) { 
      throw 'Error'; 
     } else { 
      res.json(doc); 
     } 
    }); 
}); 

module.exports = router; 

而且user.js是這樣的:

var express = require('express'); 
var router = express.Router(); 

/* GET users listing. */ 
router.get('/', function(req, res, next) { 
    res.send('respond with a resource'); 
}); 

module.exports = router; 

我試圖找到我的解決方案SO,但不能。隨時告訴我,如果我需要提供任何其他文件。任何幫助?

+2

安置自己的索引和用戶線路 –

+0

ü意味着'路線/ index.js'和'路線裏面的代碼/ users.js'文件? – Saani

+0

是的,因爲這就是問題的原因 –

回答

2

您應該像在user.js那樣在您的index.js中定義路線。

app.use('/', routes)在您的代碼中預計routesRouter的實例,但是您正在使用函數而不是該函數導出對象。

所以你index.js文件應具有以下結構:

var express = require('express'); 
var router = express.Router(); 

router.get("/", function (req, res) { 
    res.render('index'); 
}); 

router.get("/list", function(req, res) {/*list implementation*/}); 

.... 

module.exports = router; 
+0

讓我更清楚一點,我引用了這篇文章(http://www.ibm.com/developerworks/library/wa-nodejs-polling-app/)並複製了代碼,並且它說要注入THAT代碼在我的index.js,但它似乎並沒有工作。 – Saani

+0

請參考我提供的鏈接'清單13'。 – Saani

+0

如果你已經正確地閱讀,他們正在'app.js' app.get('/ polls/polls',routes.list)使用'routes.js';' –