Is this the correct way to connect to a database ?, but it worked for me ... many times I find answers: "this form is obsolete", and my question is that, this is a good way to connect to a bd and to make queries? (or the only one), I found it very simple and fast I would like to know if it is the right way only.
This is my server file where I make inquiries and I receive the client's requests:
var app = require('express')();
var express = require('express');
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static('./public'));
var users = [];
var nicks = new Array();
var mysql = require("mysql");
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "prueba"
});
con.connect(function(err) {
if (err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
con.query(
'SELECT * FROM canales',
function(err, rows) {
if (err) throw err;
console.log(rows);
}
);
io.sockets.on('connection', function(socket) {
socket.on('room', function(room) {
socket.join(room);
});
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});