Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node.js Asynch options from Nettuts
// Taken from: http://net.tutsplus.com/tutorials/javascript-ajax/managing-the-asynchronous-nature-of-node-js/
// STANDARD
app.get('/login', function (req, res) {
sql.query('SELECT 1 FROM users WHERE name = ?;', [ req.param('username') ], function (error, rows) {
if (error) {
res.writeHead(500);
return res.end();
}
if (rows.length < 1) {
res.end('Wrong username!');
} else {
sql.query('SELECT 1 FROM users WHERE name = ? && password = MD5(?);', [ req.param('username'), req.param('password') ], function (error, rows) {
if (error) {
res.writeHead(500);
return res.end();
}
if (rows.length < 1) {
res.end('Wrong password!');
} else {
sql.query('SELECT * FROM userdata WHERE name = ?;', [ req.param('username') ], function (error, rows) {
if (error) {
res.writeHead(500);
return res.end();
}
req.session.username = req.param('username');
req.session.data = rows[0];
res.rediect('/userarea');
});
}
});
}
});
});
// CALLBACK NAMING
var util = require('util');
function Login(username, password) {
function _checkForErrors(error, rows, reason) {
if (error) {
this.emit('error', error);
return true;
}
if (rows.length < 1) {
this.emit('failure', reason);
return true;
}
return false;
}
function _checkUsername(error, rows) {
if (_checkForErrors(error, rows, 'username')) {
return false;
} else {
sql.query('SELECT 1 FROM users WHERE name = ? && password = MD5(?);', [ username, password ], _checkPassword);
}
}
function _checkPassword(error, rows) {
if (_checkForErrors(error, rows, 'password')) {
return false;
} else {
sql.query('SELECT * FROM userdata WHERE name = ?;', [ username ], _getData);
}
}
function _getData(error, rows) {
if (_checkForErrors(error, rows)) {
return false;
} else {
this.emit('success', rows[0]);
}
}
function perform() {
sql.query('SELECT 1 FROM users WHERE name = ?;', [ username ], _checkUsername);
}
this.perform = perform;
}
util.inherits(Login, EventEmitter);
module.exports = Login;
// USAGE
var Login = require('./login.js');
app.get('/login', function (req, res) {
var login = new Login(req.param('username'), req.param('password));
login.on('error', function (error) {
res.writeHead(500);
res.end();
});
login.on('failure', function (reason) {
if (reason == 'username') {
res.end('Wrong username!');
} else if (reason == 'password') {
res.end('Wrong password!');
}
});
login.on('success', function (data) {
req.session.username = req.param('username');
req.session.data = data;
res.redirect('/userarea');
});
login.perform();
});
// PROMISES
var Q = require('q');
app.get('/login', function (req, res) {
Q.ninvoke('query', sql, 'SELECT 1 FROM users WHERE name = ?;', [ req.param('username') ])
.then(function (rows) {
if (rows.length < 1) {
res.end('Wrong username!');
} else {
return Q.ninvoke('query', sql, 'SELECT 1 FROM users WHERE name = ? && password = MD5(?);', [ req.param('username'), req.param('password') ]);
}
})
.then(function (rows) {
if (rows.length < 1) {
res.end('Wrong password!');
} else {
return Q.ninvoke('query', sql, 'SELECT * FROM userdata WHERE name = ?;', [ req.param('username') ]);
}
})
.then(function (rows) {
req.session.username = req.param('username');
req.session.data = rows[0];
res.rediect('/userarea');
})
.catch(function (error) {
res.writeHead(500);
res.end();
})
.done();
});
// STEP LIBRARY
var step = require('step');
app.get('/login', function (req, res) {
step(
function start() {
sql.query('SELECT 1 FROM users WHERE name = ?;', [ req.param('username') ], this);
},
function checkUsername(error, rows) {
if (error) {
res.writeHead(500);
return res.end();
}
if (rows.length < 1) {
res.end('Wrong username!');
} else {
sql.query('SELECT 1 FROM users WHERE name = ? && password = MD5(?);', [ req.param('username'), req.param('password') ], this);
}
},
function checkPassword(error, rows) {
if (error) {
res.writeHead(500);
return res.end();
}
if (rows.length < 1) {
res.end('Wrong password!');
} else {
sql.query('SELECT * FROM userdata WHERE name = ?;', [ req.param('username') ], this);
}
},
function (error, rows) {
if (error) {
res.writeHead(500);
return res.end();
}
req.session.username = req.param('username');
req.session.data = rows[0];
res.rediect('/userarea');
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment