'散列'字符串不是函数

时间:2015-05-12 16:47:56

标签: javascript node.js mongodb mongoose

有人可以告诉我为什么我似乎无法通过这里的登录页面?

app.configure(function() {
    this.engine('ejs', require('ejs-locals'));
    this.set('views', path.join(__dirname, 'views'));
    this.set('view engine', 'ejs');
    this.use(express.static(path.join(__dirname, '/public')));
    this.use(express.static(path.join(__dirname, '/views/login')));
    this.use(express.static(path.join(__dirname, '/views/el')));
    // Allow parsing cookies from request headers
    this.use(express.cookieParser());

    this.use(express.session({
        "secret": sessionSecret,
        "store": sessionStore
    }));
    // Allow parsing form data
    this.use(express.bodyParser());
});
app.configure('development', function() {
    this.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
});
app.configure('production', function() {
    this.use(express.errorHandler());
});

function authenticate(name, password, fn) {
    if (!module.parent) console.log('authenticating %s:%s', name, password);

    User.findOne({
            username: name
        },

        function(err, user) {
            if (user) {
                if (err) return fn(new Error('cannot find user'));
                hash(password, user.salt, function(err, hash) {
                    if (err) return fn(err);
                    if (hash == user.hash) return fn(null, user);
                    fn(new Error('invalid password'));
                });
            } else {
                return fn(new Error('cannot find user'));
            }
        });

}

function requiredAuthentication(req, res, next) {
    if (req.session.user) {
        next();
    } else {
        req.session.error = 'Access denied!';
        res.redirect('/login');
    }
}

function userExist(req, res, next) {
    User.count({
        username: req.body.username
    }, function(err, count) {
        if (count === 0) {
            next();
        } else {
            req.session.error = "User Exists"
            res.redirect("/signup");
        }
    });
}

//Routes

app.get("/", function(req, res) {

    if (req.session.user) {
        res.send("Welcome " + req.session.user.username + "<br>" + "<a href='/logout'>logout</a>");
    } else {
        res.render("./login/login");
    }
});

app.get("/signup", function(req, res) {
    if (req.session.user) {
        res.redirect("/");
    } else {
        res.render("./login/Signup");
    }
});

app.post("/signup", userExist, function(req, res) {
    var password = req.body.password;
    var username = req.body.username;

    hash(pass, function(err, salt, hash) {
        if (err) throw err;
        var user = new User({
            username: username,
            salt: salt,
            hash: hash,
        }).save(function(err, newUser) {
            if (err) throw err;
            authenticate(newUser.username, password, function(err, user) {
                if (user) {
                    req.session.regenerate(function() {
                        req.session.user = user;
                        req.session.success = 'Authenticated as ' + user.username + ' click to <a href="/logout">logout</a>. ' + ' You may now access <a href="/">/</a>.';
                        res.redirect('/');
                    });
                }
            });
        });
    });
});

app.get("/login", function(req, res) {
    res.render("./login/login");
});

app.post("/login", function(req, res) {
    authenticate(req.body.username, req.body.password, function(err, user) {
        if (user) {

            req.session.regenerate(function() {

                req.session.user = user;
                req.session.success = 'Authenticated as ' + user.username + ' click to <a href="/logout">logout</a>. ' + ' You may now access <a href="/restricted">/restricted</a>.';
                res.redirect('/');
            });
        } else {
            req.session.error = 'Authentication failed, please check your ' + ' username and password.';
            res.redirect('/login');
        }
    });
});

app.get('/logout', function(req, res) {
    req.session.destroy(function() {
        res.redirect('/');
    });
});

app.get('/profile', requiredAuthentication, function(req, res) {
    res.send('Profile page of ' + req.session.user.username + '<br>' + ' click to <a href="/logout">logout</a>');
});

当我用node编译我的server.js文件并转到localhost时,会出现登录页面。当我输入我已插入Mongo DB的用户名和密码时,收到以下消息:

Unable to connect

Firefox can't establish a connection to the server at localhost.

    The site could be temporarily unavailable or too busy. Try again in a few moments.
    If you are unable to load any pages, check your computer's network connection.
    If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

我的Node提示符显示以下内容:

/home/ghanem/server/www/server.js:210
           hash(password, user.salt, function (err, hash) {
           ^ TypeError: string is not a function
    at /home/ghanem/server/www/server.js:210:12
    at /home/ghanem/server/www/node_modules/mongoose/lib/query.js:1170:16
    at /home/ghanem/server/www/node_modules/mongoose/node_modules/kareem/index.js:103:16
    at process._tickCallback (node.js:415:13)

0 个答案:

没有答案