Nodejs + Passport + MySQL

我想弄清楚如何使用nodejs + Passport + MySQL。 看起来好像每个教程都使用mongoDB,我不想这样做。 事实上,这种types的快速search会产生像( http://nodejsrocks.blogspot.com/2012/04/nodejs-expressjs-mysql.html )这样的网页和一个人(youtube video)( https:// www .youtube.com / watch?v = jGBbMVJx3h0 )除了login之外什么都不做,谁知道他真的在用什么,但是页面已经有了3K +的视图。 我希望一些开发人员能够看到这个,也许有一种用途,就像一个全面的非MVCtypes的事情与MySQL。 我之所以这样做,是因为我只想获得iOS和Android的function,而不需要大量的脚手架开销。 只是数据库和服务器端脚本处理查询和返回的JSON对象的手机。

所以呢,这样说呢,能有一个有实际经验的人请帮帮我(而世界其他地方都没有深入的教程,因为我们没有使用mongoDB和完整的脚手架来做类似的事情) 。

我为“TwitterStrategy”设置的表格是用户(id(PK),用户名,电子邮件,salt,密码)和twitterusers(id),名称,屏幕名称,位置,说明,url,img, tokensecret)。

这是我想要从一个单一的main.js文件去的代码。 我知道这不是最佳做法,而且我计划在稍后清理它,但现在,我想了解我所缺less的东西,并使其工作。 如果有人能帮忙,我将非常感激,而且我很确定其他人也会觉得这很有用。 谢谢。

var http = require('http'), mysql = require('mysql'), url = require('url'), crypto = require('crypto'), express = require('express'), flash = require('connect-flash'), passport = require('passport'), TwitterStrategy = require('passport-twitter').Strategy; var db = mysql.createConnection({ host : "****", user : "****", password : "****", port : '****', database : '****' }); // Connect the connection to DB and throw error if exists db.connect(function(err) { if (err) { console.error('Error connecting to db'); console.error(err); return; } console.log('Database connected'); }); var TWITTER_CONSUMER_KEY = "****"; var TWITTER_CONSUMER_SECRET = "****"; passport.use(new TwitterStrategy({ consumerKey: TWITTER_CONSUMER_KEY, consumerSecret: TWITTER_CONSUMER_SECRET, callbackURL: 'http://127.0.0.1:3000/auth/twitter/callback'}, function(accessToken, refreshToken, profile, done) { //db.query(SELECT ........ FROM ...... WHERE ........, function (err, user){ if (err) { console.log(err); } if (!err && user != null){ done(null, result); } else { console.log(result); } }) }); } )); passport.serializeUser(function(user, done) { console.log('serializeUser: ' + user.id); done(null, user.id); }); passport.deserializeUser(function(id, done) { db.query('SELECT * FROM users WHERE id = ' + id, function(err, result) { if (err){ console.log(err); } else { console.log(result); } if (!err) { done(null, result); } else { done(err, null); } }); }); var app = express(); app.set(function(){ // app.set('views', __dirname + '/views'); // Definitely for some views which aren't being used here // app.set('view engine', 'jade'); // Using jade for views, not used // app.use(express.favicon()); // Not really sure this is important, should be web only app.use(express.logger('dev')); // Again, not really sure this is important app.use(express.bodyParser()); // Have no idea what this is used for app.use(express.methodOverride()); // Same no Fn clue app.use(express.cookieParser('what the F')); app.use(express.session()); app.use(passport.initialize()); app.use(passport.session()); app.use(flash()); // app.use(app.router); // Here again we are defining our routes in main, so shouldn't need this. // app.use(express.static(__dirname + '/public')); }); var server = http.createServer(function(req, res) { console.log('url: ' + req.url); var params = url.parse(req.url, true) var path = params.pathname; if (path == '/signup') { console.log("User signing up"); onSignUp(params, res); } else if (path == '/signin') { console.log("User signing in"); onSignIn(params, res); } else if (path == '/auth/twitter'){ passport.authenticate('twitter'), function(req, res){ console.log('Twitter User Created or Signed In'); } } }); //Keep server alive and listening to requests with DB connected also server.listen(3000); 

我错过了另一个validation表? 什么是我需要把MySQL的声明中的点是这样的,我可以find用户,并从用户请求传递什么参数来获取查询,即什么是我见过的这个oauth ID教程是从似乎是用户到Twitter的授权传递? 另外,我应该从Twitter的这个callback中期待什么? 无论如何,我会很高兴发布所有这个地方给其他人看看,只要我有一个解决scheme,使我们所有人使用MySQL和节点不要被遗漏,不得不search谷歌,find似乎好像它应该是现成的,而不是相同的确切nodejs + mongoDB +快速教程的副本(许多过时的除了苏格兰io,如果你想使用mongo,看起来非常好…我可以添加亚马逊上的实例在低端运行大约每月279美元),这是浮动的,几乎任何有“教程”的人都可以重新分配。 再次感谢。

尝试在process.nextTick下包装策略函数,例如,

 passport.use(new TwitterStrategy({ consumerKey: TWITTER_CONSUMER_KEY, consumerSecret: TWITTER_CONSUMER_SECRET, callbackURL: 'http://127.0.0.1:3000/auth/twitter/callback'}, function(accessToken, refreshToken, profile, done) { process.nextTick(function(){ // this is where you put logic to check the profile sent from twitter already in your DB or not, // its totally up to you whether you keep a separate auth table for it or not // NB: there will be some unique value in profile that can be used for next references db.query(SELECT ........ FROM ...... WHERE ........, function (err, user){ if (err) { console.log(err); } if (!err && user != null){ done(null, result); } else { console.log(result); } }) }); }); } )); 

你还必须有一个接受callback的路线,例如,

 app.get('/auth/twitter/callback', function(req, res, next) { passport.authenticate('twitter', { }, function(err, user) { // the result you send from the strategy function will be here // do anything you like with the user send } )(req, res, next); }); 

希望它使事情更清楚。