2014-11-06 169 views
1

我創建了一個簡單的項目,包含3個頁面:主頁,聯繫人和關於。node.JS快遞護照路由

一切正常,然後我想添加用戶註冊與mongo。

所以我跟着這個tutorial

但是,當我添加薩姆新的路線,我不能存取權限任何頁面。

這是我index.js時,我可以存取權限我

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

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



/* GET contact page. */ 
router.get('/contact', function(req, res) { 
    res.render('contact'); 
}); 

/* POST contact page (submit). */ 
router.post('/contact', function(req, res) { 
    res.render('contact', { name: req.param('name') }); 
}); 

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

/* POST home page (submit). */ 
router.post('/home', function(req, res) { 
    res.render('home', { comm: req.param('comm') }); 
}); 


/* GET about page. */ 
router.get('/about', function(req, res) { 
    res.render('about'); 
}); 


module.exports = router; 

所有簡單和工作3頁。 而現在,它就像是:

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

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



/* GET contact page. */ 
router.get('/contact', function(req, res) { 
    res.render('contact'); 
}); 

/* POST contact page (submit). */ 
router.post('/contact', function(req, res) { 
    res.render('contact', { name: req.param('name') }); 
}); 

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

/* POST home page (submit). */ 
router.post('/home', function(req, res) { 
    res.render('home', { comm: req.param('comm') }); 
}); 


/* GET about page. */ 
router.get('/about', function(req, res) { 
    res.render('about'); 
}); 


module.exports = router; 

module.exports = function(passport){ 

    // /* GET login page. */ 
    // router.get('/', function(req, res) { 
    // // Display the Login page with any flash message, if any 
    // res.render('index', { message: req.flash('message') }); 
    // }); 

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

    /* GET Registration Page */ 
    router.get('/signup', function(req, res){ 
    res.render('register',{message: req.flash('message')}); 
    }); 

    /* Handle Registration POST */ 
    router.post('/signup', passport.authenticate('signup', { 
    successRedirect: '/home', 
    failureRedirect: '/signup', 
    failureFlash : true 
    })); 

    return 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 swig   = require('swig'); 

var dbConfig = require('./db.js'); 
var mongoose = require('mongoose'); 
mongoose.connect(dbConfig.url); 

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

var app = express(); 

// Configuring Passport 
var passport = require('passport'); 
var expressSession = require('express-session'); 
app.use(expressSession({secret: 'mySecretKey'})); 
app.use(passport.initialize()); 
app.use(passport.session()); 


// view engine setup 
app.engine('html', swig.renderFile); 

app.set('view engine', 'html'); 
app.set('views', path.join(__dirname, 'views')); 

// view cache 
app.set('view cache', false); 
swig.setDefaults({ cache: false }); 

app.use(favicon()); 
app.use(logger('dev')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded()); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 

app.use('/', routes); 

/// catch 404 and forwarding 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; 
+0

顯示,調用此模塊(即,app.js) – ikumen 2014-11-06 14:42:46

+0

app.js在編輯 – F4Ke 2014-11-06 14:45:52

+0

我很欣賞的代碼這是一箇舊的帖子,但如果你仍然關於:你是否有兩次分配model.export的原因?我是新來的節點,所以也許這是合法的,但從js pov看來,你似乎覆蓋了最初模型中的所有東西。出口 – Glitcher 2016-12-27 23:28:57

回答

2

你的模塊需要一個passport對象

module.exports = function(passport){ 

但你不能在app.js護照對象調用它

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

也,您需要在調用路由模塊之前定義護照。那麼,到底你需要修改app.js看起來是這樣的:

var passport = require('passport'); 
var expressSession = require('express-session'); 
app.use(expressSession({secret: 'mySecretKey'})); 
app.use(passport.initialize()); 
app.use(passport.session()); 

... 
var routes = require('./routes/index')(passport); 
... 
app.use('/', routes); 
+0

它似乎工作正常!泰 – F4Ke 2014-11-06 15:01:22