1

在我的情况下,只要我不登录,GET / 就可以工作,但是当我通过访问 GET /login 成功登录时,它会尝试重定向到 / 但服务器没有响应,随后服务器对 GET / 没有任何响应,直到服务器重新启动. 这种情况仅在浏览器中观察到。谁能解释我

/**
 * Module dependencies.
 */

var express = require('express')
 , routes = require('./routes')
 , user = require('./routes/user')
 , http = require('http')
 , path = require('path')
 , cons = require('consolidate');

var passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;

var app = express();
var User=require('./class/User');
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session({ secret: 'keyboard cat' }));
//// passport auth ///////////
app.use(passport.initialize());
app.use(passport.session());
///////////////////////////////
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});

// These lines are required to register underscore template
var _ = require('underscore');
app.engine('html', cons.underscore);

app.configure('development', function(){
  app.use(express.errorHandler());
});


passport.use(new LocalStrategy(
  function(username, password, done) {
    var user=User.auth(username, password);

    if(user)
    {
      console.log("success");
      return done(null, user); 
    }
    else
    { console.log("error");
        return done(null, false, { message: 'Incorrect credentials.' });}
    /*(User.findOne({ username: username }, function(err, user) {
    if (err) { return done(err); }
    if (!user) {
      return done(null, false, { message: 'Incorrect username.' });
    }
    if (!user.validPassword(password)) {
      return done(null, false, { message: 'Incorrect GET /login 200 30ms - 313
      password.' });
    }
    return done(null, user);
  });*/
 }
));
passport.serializeUser(function(user, done) {
done(null, user.userid);
});

passport.deserializeUser(function(id, done) {
/*findById(id, function (err, user) {
done(err, user);
});*/
});

app.post('/login',
passport.authenticate('local', { successRedirect: '/',
                              failureRedirect: '/login',
                              failureFlash: false })
);

app.get('/', routes.index);
app.get('/login', routes.login);
app.get('/users', user.list);



http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});
4

0 回答 0