Есть такой тестовый код:
........
try {
var mysql = require('mysql');
var express = require('express');
var app = express();
var io = require('socket.io').listen(app.listen(PORT));
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
var username = req.query.username;
var password = req.query.password
if (username && password) {
res.sendfile(__dirname + '/html/index.html');
io.on('connection',function (client) {
client.username = username;
client.password = password;
setUsersList('1,2,3');
});
}
else {
res.writeHead(401, {"Content-Type": "text/html; charset=utf-8"});
res.end("Ошибка авторизации!");
}
});
app.get('/*', function (req, res) {
res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
res.end("Ошибка!");
});
}
catch (e){console.log('!ERR: '+e.message);}
//коннектор mysql
function connectormysql() {
return connection = mysql.createConnection({
host: MySQL_host,
user: MySQL_user,
password: MySQL_pass,
database: MySQL_db
});
};
function setUsersList(users) {
var conn = connectormysql();
conn.connect(function (err) {
if(err)console.log('==='+err.message+'===');
conn.end();
return;
});
conn.query('SELECT id,name FROM users WHERE id IN('+users+')',function (err,result) {
console.log(result);
conn.destroy();
});
}
Вот что получается после подключения пользователя:
[ RowDataPacket { id: 1, name: 'Иван' },
RowDataPacket { id: 2, name: 'Степан' },
RowDataPacket { id: 3, name: 'Рубен' } ]
C:\test\node_modules\mysql\lib\protocol\Parser.js:80
throw err; // Rethrow non-MySQL errors
^
Error: This socket is closed
at Socket._writeGeneric (net.js:679:19)
at Socket._write (net.js:730:8)
at doWrite (_stream_writable.js:333:12)
at writeOrBuffer (_stream_writable.js:319:5)
at Socket.Writable.write (_stream_writable.js:245:11)
at Socket.write (net.js:657:40)
at Protocol.<anonymous> (C:\test\node_modules\mysql\lib\Connection.js:100:26)
at emitOne (events.js:96:13)
at Protocol.emit (events.js:188:7)
at Protocol._emitPacket (C:\test\node_modules\mysql\lib\protocol\Protocol.js:293:8)
--------------------
at Protocol._enqueue (C:\test\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.quit (C:\test\node_modules\mysql\lib\protocol\Protocol.js:92:23)
at Connection.end (C:\test\node_modules\mysql\lib\Connection.js:249:18)
at Handshake._callback (C:\test\server.js:61:14)
at Handshake.Sequence.end (C:\test\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
at Handshake.Sequence.OkPacket (C:\test\node_modules\mysql\lib\protocol\sequences\Sequence.js:97:8)
at Protocol._parsePacket (C:\test\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (C:\test\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\test\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\test\node_modules\mysql\lib\Connection.js:103:28)
Я новичок в этом деле. Помогите, что я делаю не так?