2014-06-15 94 views
0

我試圖做這行node.js表示變量undefined?

router.post(「/登錄」,passport.authenticate ...)

和路由器是在我的目錄中的文件夾中的路由器。我已經將它作爲模塊導出,並在我的app.js文件中使用它。

在app.js我有我需要在這個順序:

var passport = require('passport'); 
var routes = require('./routes/index'); 
var users = require('./routes/users'); 
var User = require('./models/models.js') 

所以不應路由能夠訪問的護照嗎?

這是我的路線/指數

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

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

router.post('/login', passport.authenticate('local', { 
    successRedirect: '/home', 
    failureRedirect: '/login', 
    failureFlash: true 
})); 

router.get('/login', function(req, res, next) { 
    var errors = req.flash('error'); 
}) 
module.exports = router; 

這是我app.js的開始

var express = require('express'); 
var path = require('path'); 
var favicon = require('static-favicon'); 
var logger = require('morgan'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 
var mongoose = require('mongoose'); 
var passport = require('passport'); 
var routes = require('./routes/index'); 
var users = require('./routes/users'); 
var User = require('./models/models.js'); 
var PassportLocalStrategy = require('passport-local'); 
+1

後您用來導出模塊的代碼。 – Michael

+0

你有沒有試過'require(「./config/passport」)(護照);'? – royhowie

+0

添加了代碼,沒有要求配置,因爲我沒有這樣的文件夾 – user3340037

回答

0

需要重新包括護照模塊內的變量。

0

var passport = require('passport');添加到您的routes/index.js文件的頂部。

或者,包你的路由器在module.exports = function(passport) {};並通過passport進入routes要求在app.js

像這樣:var routes = require('./routes/index')(passport);