`
ynial
  • 浏览: 228142 次
  • 来自: ...
社区版块
存档分类
最新评论

node.js & postgreSQL 初体验[二]

阅读更多
局部代码:模拟一个小登录的测试!

刚开始的时候忘记这NODE.JS异步问题了,怎么测都不行!

后来一顿回调啊~ OK了~!  这种方法代码的可读性很差,嵌套太多了。等等吧

等过几天再研究一下 ,解决这异步的问题!~
index.js
var um = require('./modules/user-manager');

exports.index = function(req, res){
	res.render('index', { title: 'Test Login DEMO' });
	um.findUserByEmail('tom@gmail.com', function(user) {
		if (user == null) {
			console.log('no user');
		} else {
			console.log(user.email);
			um.getMd5Password('x', function(password) {
				console.log('userpassword', user.password);
				console.log('input password', password);
				if (user.password != password) {
					console.log('password error');
				} else {
					console.log('OK');
				}
			});
		}
	});
};


./modules/user-manager

var pg = require('pg'); 

var conString = "postgres://postgres:password@localhost/test";  
var client = new pg.Client(conString);  
client.connect();


var userManager = {};

userManager.findUserByEmail = function(email, callback) {
	var query = client.query('SELECT * FROM person WHERE email = $1', [email]);
	query.on('row', function(row, result) {
		result.addRow(row);
	});
	query.on('end', function(result) {
		if (result.rowCount == 0) {
			callback(null);
		} else {
			callback(result.rows[0]);
		}
	});
}


userManager.getMd5Password = function(password, callback) {
	var query = client.query('SELECT md5($1) as p', [password]);
	query.on('row', function(row, result) {
		callback(row.p);
	});
}

module.exports = userManager;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics