Reputation: 115
I'm running node.js code on my server to get data from mysql database.
This is my code:
var express = require("express");
var mysql = require('mysql');
var app = express();
var pool = mysql.createPool({
connectionLimit : 100, //important
host : 'localhost',
user : 'root',
password : '',
database : 'coinflip',
debug : true
});
function handle_database(req,res) {
pool.getConnection(function(err,connection){
if (err) {
connection.release();
res.json({"code" : 100, "status" : "Error in connection database"});
return;
}
console.log('connected as id ' + connection.threadId);
connection.query("SELECT * FROM games WHERE winner=''",function(err,rows){
connection.release();
if(!err) {
res.json(rows);
}
});
connection.on('error', function(err) {
res.json({"code" : 100, "status" : "Error in connection database"});
return;
});
});
}
app.get("/",function(req,res){-
handle_database(req,res);
});
app.listen(3000);
It gets all data I want from database, but I want it to auto-refresh without refreshing whole site. By auto-refresh I mean if there is any change (update
, insert
), display them on site.
Is that possible?
Upvotes: 2
Views: 10976
Reputation: 585
Don't know if it's still needed.. But there is a simple libary https://www.npmjs.com/package/mysql-events
Upvotes: 6