刚开始写node,感觉写出来的代码太丑了,可读性也不强。下面这段代码是一个简单的注册逻辑,用了Promise,希望大神拍砖,给一些风格或者是写法上的建议:
register: function(req, res) {
var sha1 = require('crypto').createHash('sha1');
var email = req.body.email;
var password = sha1.update(req.body.password).digest('hex');
var connect = mysql.connect(__dirname + '/../mysql.json', 'dbname');
var sql = 'into user(email, password) value (?, ?)';
emailAlreadExists(email).then(function() {
addUser(email, password)
.then(function(id) {
res.send(res.send(lib.genAjaxRet(0, 'success', {id: id})));
});
}, function() {
res.send(lib.genAjaxRet(10001, lib.s('EMAIL_EXISTS')))
});
}