3

我创建了一个简单的项目,有 3 个页面:主页、联系人和关于。

一切正常,然后我想用 mongo 添加用户注册。

所以我跟着这个教程

但是,当我添加一些新路线时,我无法访问更多页面。

这是我index.js可以访问我的 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;

一切都简单而有效。现在,就像这样:

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;
4

1 回答 1

5

您的模块需要一个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);
于 2014-11-06T14:51:32.390 回答