如何使用快速路由器正确调用passport.js功能?

时间:2015-05-01 14:17:23

标签: javascript node.js express passport.js passport-local

我试图将passport.authenticate函数作为对我的注册视图的发布请求的响应。我一直在关注this tutorial here以了解passport.js并表达但已完成第一页说明并对express.js 4.0进行必要的更改我尝试访问的每个页面返回错误500和显示以下堆栈跟踪:

var express = require('express');
var bodyParser = require('body-parser');
var router = express.Router();

module.exports = function(app, passport) {
  /* GET home page. */
  router.get('/', function(req, res, next) {
    res.render('index', {
      title: 'Home Page'
    });
  });

  /* GET about page */
  router.get('/about', function(req, res, next) {
    res.render('about', {
      title: 'About Us'
    });
  });

  /* GET contact page */
  router.get('/contact', function(req, res, next) {
    res.render('contact', {
      title: 'Contact Us'
    });
  });

  /* GET login page */
  router.get('/login', function(req, res, next) {
    res.render('login', {
      title: 'User Login',
      message: req.flash('loginMessage')
    });
  });

  /* GET register page */
  router.get('/register', function(req, res, next) {
    res.render('register', {
      title: 'User Registration',
      message: req.flash('signupMessage')
    });
  });

  /* POST register page */
  router.post('/register', passport.authenticate('local', {
    successRedirect : '/',
    failureRedirect : '/register',
    failureFlash    : true
  }));

  /* Log Out */

  router.get('/logout', function(req, res, next) {
    req.logout();
    res.redirect('/');
  });

  /* 404 - page not found */
  router.get('*', function(req, res){
    res.render('404', {
      title: '404 - Not Found'
    });
  });

  // Route middleware to make sure a user is logged in
  function isLoggedIn(req, res, next) {
    if (req.isAuthenticated())
      return next();
    res.redirect('/');
  };
};

以下是/routes/index.js的代码:

var express       = require('express');
var path          = require('path');
var logger        = require('morgan');
var mongoose      = require('mongoose');
var bodyParser    = require('body-parser');
var cookieParser  = require('cookie-parser');
var session       = require('express-session');
var passport      = require('passport');
var flash         = require('connect-flash');

// Database

var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
require('./config/passport')(passport); 

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

var port = process.env.PORT || 3000;
var app = express();

// Express configuration
app.use(logger('dev')); // log every request to console
app.use(bodyParser.json()); // get information from html forms
  app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser()); // read cookies (needed for auth)
app.use(express.static(path.join(__dirname, 'public')));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
if (app.get('env') === 'development') {
  app.locals.pretty = true;
}

// Passport configuration

app.use(session({ secret: 'MySecretHere',
                  saveUninitialized: true,
                  resave: true})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

app.use('/', routes);

// 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: {}
    });
});

app.listen(port);
console.log('Server running on ' + port);

这是我的server.js:

$("input[type='checkbox'][name^='category_']").each(function() { this.checked = false; });

1 个答案:

答案 0 :(得分:3)

您收到错误的原因是您导出的函数将#include <stdlib.h> #include <string.h> struct pointer_person { char *key; char *color; int age; }; struct pointer_person deep_copy(struct pointer_person p) { struct pointer_person retval; retval.key = malloc(strlen(p.key) + 1); strcpy(retval.key, p.key); retval.color = malloc(strlen(p.color) + 1); strcpy(retval.color, p.color); retval.age = p->age; return retval; } int main() { struct pointer_person p; struct pointer_person pc; p.key = malloc(50); strcpy(p.key, "A key"); p.color = malloc(20); strcpy(p.color, "A colour"); p.key = 42; pc = deep_copy(p); /* do stuff with pc and c */ free(p.key); free(p.color); free(pc.key); free(pc.color); return 0; } malloc()作为http处理程序的参数,但是当您在app中需要它时没有通过passportserver.js

您需要以某种方式将passport传递给require。 您可以将路径文件更改为 如果您使用的是app

passport

如果不使用express.Router,您可以同时传递 var router = express.Router(); module.exports = function(passport) { // init your routes with passport and router router.post('/' function(req, res) { //... }); // make sure to return router in the function though return router; } express.Router,只需将处理程序附加到app

passport

然后在server.js

app