Mongodb - 500:内部服务器错误

时间:2016-03-29 05:51:07

标签: node.js mongodb express passport-local

我尝试使用Advanced Rest Client来查看我的MongoDB是否已正确设置,但它一直给我500:内部服务器错误。

这是我的app.js文件:

var express = require('express');
var path = require('path');
var logger = require('morgan');
var passport = require('passport');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/emedb');
var app = express();
var routes = require('./routes/index');
var users = require('./routes/users');
var authenticate = require('./routes/authenticate')(passport);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(session({
    secret: '<never-tell>',
    saveUninitialized: true,
    resave: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(passport.initialize());
app.use(passport.session());
// Initialize models
require('./models/models.js')
// Initialize Passport
var initPassport = require('./passport-init');
initPassport(passport);
app.use('/', routes);
app.use('/users', users);
app.use('/auth', authenticate);

我有一个&#39; passport-init.js&#39;应用程序的根目录中的文件:

var mongoose = require('mongoose');
var Users = mongoose.model('Users');
var LocalStrategy   = require('passport-local').Strategy;
var bCrypt = require('bcrypt-nodejs');


module.exports = function(passport){

// Passport needs to be able to serialize and deserialize users to support persistent login sessions
passport.serializeUser(function(user, done) {
    // tell passport which id to use for user
    console.log('serializing user:', user._id);
    return done(null, user._id);
});

passport.deserializeUser(function(id, done) {
    //returns user object back
    Users.findById(id, function(err, user){
        if (err){
            return done(err, false);
        }
        if(!user){
            return done('Username not found', false);
        }
        // user object found - provided back to passport
        return done(user, true);
    });
});

passport.use('login', new LocalStrategy({
        passReqToCallback : true
    },
    function(req, username, password, done) { 


        Users.findOne({username: username}, function(err, user){
            if(err){
                return done(err, false);
            }

            // no user with username
            if (!user){
                return done('uesr ' + username + ' not found', false);
            }

            // incorrect password
            if(!isValidPassword(user, password)){
                return done('incorrect password', false);
            }

            return done(null, user);
        });
    }
));

passport.use('signup', new LocalStrategy({
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },

    function(req, username, password, done) {

        // find a user in mongo with provided username
        Users.findOne({ 'username' :  username }, function(err, user) {
            // In case of any error, return using the done method
            if (err){
                return done(err);
            }
            if(user){
                // user already signed up
                return done('username taken', false);
            }
            else{
            // no user exists - create new user
            var user = new Users();
            user.username = username;
            user.password = createHash(password);

                user.save(function(err, user){
                    if(err){
                        return done(err, false);
                    }
                console.log('Thank You For Signing Up');
                return done(null, user);
            });
            }
        });
    })
);  


/*              });
            };
        });
    };
*/



var isValidPassword = function(user, password){
    return bCrypt.compareSync(password, user.password);
};
// Generates hash using bCrypt
var createHash = function(password){
    return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
};


};

而且,我有一个名为models的文件夹,其中包含&model; model.js&#39;文件包含:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userSchema = new mongoose.Schema({
  username: String,
  password: String //hash created from password

});

mongoose.model("Users", userSchema);

请原谅大量的代码,我有点像菜鸟,并且真的不知道我哪里出错了。

0 个答案:

没有答案