I tried the database login with HTML5+MySQL+Node.js+socket.io but failed. Please help. I want to login from HTML5 page to MySQL database with Node.js and socket.io
I have a HTML5 page with 2 text box user name and password and a LogIn button. user want to login from HTML5 page with user/password send to server and the server receive the user/password and validate with mysql database and return the result.
My Code: server_test.js
var io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/lib/socket.io');
var socket = io.listen(8124);
socket.sockets.on('connection',function(socket){
socket.on('login', function(data, usr, pass){
var mysql = require('mysql');
var TEST_DATABASE = 'employee';
var TEST_TABLE = 'tblusers';
var client = mysql.createClient({
user: 'root',
password: 'secret10',
});
client.query('USE '+TEST_DATABASE);
client.query(
'SELECT name FROM '+TEST_TABLE+' WHERE user = '+usr+' AND password = '+pass,
function selectCb(err, results) {
if (err) {
throw err;
}
//Emit a message to client
socket.emit('retuLogIn',{username: results[0]['name']});
client.end();
}
);
});
socket.on('disconnect', function(){
console.log('Server has disconnected');
});
});
client_test.html
<!doctype html>
<html>
<title>WebSocket Client Demo [socket.io]</title>
<script src="/json.js"></script> <!-- for ie -->
<script src="http://localhost:8124/socket.io/socket.io.js"></script>
<script>
function connect() {
try
{
var socket = io.connect('http://localhost:8124/');
socket.on("connect",function(){
document.getElementById('status').innerHTML ="Browser has connected to the app server";
});
socket.on('login', function (data, document.getElementById('txtUser').value, document.getElementById('txtPass').value) {
//alert(data.hello);
document.getElementById('status').innerHTML = 'Welcome '+data.username;
});
}
catch(err)
{
document.getElementById('status').innerHTML = err.message;
}
}
</script>
<body>
<h1>WebSocket Client Demo</h1>
<div><p id="status">Enter user and password to Log-In</p></div>
<label>User :</label>
<input id="txtUser" type="text" maxlength="10" />
<label>Password :</label>
<input id="txtPass" type="text" maxlength="10" />
<button id="connect" onClick='connect()'/>Log-In</button>
</body>
</html>
Note : I am tried with above mention code but no success. I was tried to send the userid/password to server from client with socket.send('abcd', 'ab12'). able to send but can't receive in server.
Please help me how to solve this issue. I am using MySQL database.
As an update, when I apply Joel's suggestions, I still see an error when I try to run app.js in Node.js:
C:\Program Files (x86)\nodejs>node "C:\Program Files (x86)\nodejs\app.js" info - socket.io started
C:\Program Files (x86)\nodejs\app.js:6
var client = mysql.createConnection({
^ TypeError: Object #<Object> has no method 'createConnection'
at Object.<anonymous> (C:\Program Files (x86)\nodejs\app.js:6:24)
at Module._compile (module.js:449:26)
at Object..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function._load (module.js:312:12)
at module.js:487:10
at EventEmitter._tickCallback (node.js:238:9)
C:\Program Files (x86)\nodejs>
What could be wrong with this now?
I just fixed the script as I needed something similar, here you go:
app.js:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, mysql = require('mysql')
var client = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
});
client.connect();
app.listen(3232);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.on('login', function(usr, pass){
var TEST_DATABASE = 'mysqltest';
var TEST_TABLE = 'users';
client.query('USE '+TEST_DATABASE);
client.query('SELECT name FROM '+TEST_TABLE+' WHERE user = "'+usr+'" AND password = "'+pass+'"', function(err, results) {
if (err) throw err;
console.log(results[0]); // [{1: 1}]
socket.emit('retuLogIn',results[0]['name']);
});
});
socket.on('disconnect', function(){
console.log('Server has disconnected');
});
});
And the index.html:
<html>
<title>WebSocket Client Demo [socket.io]</title>
<script src="http://localhost:3232/socket.io/socket.io.js"></script>
<script>
function connect() {
try
{
var socket = io.connect('http://localhost:3232/');
socket.on("connect",function(){
document.getElementById('status').innerHTML ="Browser has connected to the app server";
socket.emit('login', document.getElementById('txtUser').value, document.getElementById('txtPass').value);
});
socket.on('retuLogIn', function (data) {
document.getElementById('status').innerHTML = 'Welcome '+data;
});
}
catch(err)
{
document.getElementById('status').innerHTML = err.message;
}
}
</script>
<body>
<h1>WebSocket Client Demo</h1>
<div><p id="status">Enter user and password to Log-In</p></div>
<label>User :</label>
<input id="txtUser" type="text" maxlength="10" />
<label>Password :</label>
<input id="txtPass" type="text" maxlength="10" />
<button id="connect" onClick='connect()'/>Log-In</button>
</body>
</html>
You need to change stuff like mysql database name, username, password and probaly the ports if needed.
I used node.js, socket.io and node-mysql
Chandan Dey you are missing
mysql = require('mysql')
in your code, cheers