[英]node.js - connection pooling
Hello everyone, 大家好,
just wondering , is this the proper way to : 我只是想知道,这是否是正确的方法:
This below code is called everytime an user logs in, it checks his username and token, if it's a match it will open main menu page, if not it will return back to login page. 用户每次登录时都会调用以下代码,它会检查用户名和令牌,如果匹配,则会打开主菜单页面,否则将返回登录页面。
The truth is, that it's all working in Chrome, but sometimes it does not work in Firefox , it just doesn't call the connection.query() part at all.. 事实是,它们都可以在Chrome浏览器中运行,但是有时它不能在Firefox中运行,只是根本不调用connection.query()部分。
So I'm just checking with you guys , if everything is okay with the code below..Or if there is anything I could improve or change.. 所以我只是和你们一起检查,如果下面的代码一切正常,或者我可以改进或更改。
var db_pool = mysql.createPool({
host: 'localhost',
user: 'dbuser',
password: 'pass',
database: 'db_name'
});
function CheckUser(username, token)
{
db_pool.getConnection(function(err, connection)
{
console.log(" [i] Getting connection from pool. ");
var entry = 0;
var query = connection.query("SELECT token FROM users where token = '"+token+"' and user_id = '"+username+"'");
query.on('result', function(data){
entry++;
});
query.on('error',
function(err){
throw(err);
}
);
query.on('end',
function()
{
if(entry == 1)
{
console.log(" [info] User ["+username+"] authorized.");
/* DO STUFF */
}else
{
console.log(" [Error] User ["+username+"] does not have token: ["+token+"].");
return false;
}
}
);
console.log(" [i] Returning back connection to pool. ");
connection.release();
});
}
Any help is greatly appreciated, 任何帮助是极大的赞赏,
Alex 亚历克斯
I believe that the intermittent behavior is because your release() statement is in line with the connection rather than in the 'end' handler. 我相信这种间歇行为是因为您的release()语句与连接一致,而不是在“ end”处理程序中。 Here is what works for me:
这对我有用:
var mysql = require('mysql'),
config = require('./config.json');
var pool = mysql.createPool( config );
var checkUser = function(username, token) {
pool.getConnection(function(err, connection) {
if (err) throw err;
console.log(" [i] Getting connection from pool. ");
var entry = 0,
statement = 'SELECT token FROM users where token = ? and user_id = ?',
query = connection.query( statement, [ token, username ] );
query.on('result', function(data) {
console.log( data );
entry++;
});
query.on('error', function(err) {
console.log( err );
throw err;
});
query.on('end', function() {
if (entry === 1) {
console.log(" [info] User ["+username+"] authorized.");
/* DO STUFF */
} else {
console.log(" [Error] User [", username, "] does not have token [', token, ']');
}
console.log(" [i] Returning back connection to pool. ");
connection.release();
});
});
};
console.log('check the user...');
checkUser( 'test-user', 'mytoken');
I was able to test this (with an alternate statement) with no problems. 我能够毫无问题地对此进行测试(使用替代语句)。 I also put the config stuff in a separate file and replaced the string statement with parameters.
我还将配置内容放在单独的文件中,并用参数替换了字符串语句。
Hope this helps... 希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.