将用户保存到MongoDB的Passport Facebook策略

时间:2014-04-12 17:19:33

标签: node.js mongodb express passport.js

我正在尝试定期登录(使用Facebook策略)并且我一直在我的控制台中收到此错误。

  

错误:ENOENT,打开   ' /家庭/ EC2用户/ XXXX / node_modules /表达/ node_modules /连接/ node_modules /静态图标/../公共/ favicon.ico的'

方式是什么?

这是我的代码(facebook应用程序令牌在另一个js文件中):

/**
 * Module dependencies.
 */

var config = require('./oauth.js');
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var mongoose = require('mongoose');
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;

var app = express();


// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));


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

app.get('/auth/facebook/callback', passport.authenticate('facebook', {failureRedirect: '/'}), function(req, res) {
    console.log('Facebook');
    res.redirect('/success');
});

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

// MongoDB Settings
// Connection
mongoose.connect('mongodb://ec2-XX-XXX-XX-XXX.XX-XXX-2.compute.amazonaws.com/XXX', function(err) {
    if (err) {
        console.log(err);
    } else {
        console.log('Connected to the Users DB');
    }
});
// Schema
var usersSchema = mongoose.Schema({
    username: String,
    facebook_id: String
});
// Insert To DB
var insertUser = mongoose.model('Users', usersSchema);


// LogIn
passport.use(new FacebookStrategy({
    clientID: config.facebook.clientID,
    clientSecret: config.facebook.clientSecret,
    callbackURL: config.facebook.callbackURL
},
function(accessToken, refreshToken, profile, done) {
    console.log('Now Check User');
    insertUser.findOne({
        facebook_id: profile.id
    }, function(err, doc) {
        if (err) {
            console.log('user is alredy registered!');
            return done(err);
        }
        if (!insertUser) {
            var newgame = new insertUser({
                username: profile.username,
                facebook_id: profile.id
            });
            newgame.save(function(err) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('Saved');
                }
                return done(err, doc);
            });
        } else {
            //found user. Return
            console.log('user is alredy registered!');
            return done(err, doc);
        }
    });
}
));

1 个答案:

答案 0 :(得分:1)

该解决方案是badsyntax(感谢badsyntax)建议的解决方案的一部分。 最后,这解决了这个问题:

app.use(express.favicon(__dirname + '/node_modules/express/node_modules/connect/node_modules/static-favicon/favicon.ico'));