I took as skeleton the code generated by Express JS and everything works fine. The issue is that when I show my page ... / one, it shows me the right result but only for about 5 ms and the content disappears. Do you know why this happens? I would appreciate it if you can give me a hand about it. I attach the code in the route UNO.js
var router = require('express').Router();
var socket = require('../source/socket');
var conn = require('../source/conn');
var valor = [];
router.get('/', function (req, res, next) {
connection = conn.open();
connection.query("SELECT nomParam, valParam FROM tParametro WHERE idVista = '1';", function (error, results, fields) {
global.valSocket = [];
for (var i = 0; i < results.length; i++) {
valor.push(results[i].nomParam + "|" + results[i].valParam);
global.nomSocket = "valor";
global.valSocket = valor;
}
socket.emit();
res.render('uno');
});
valor = [];
connection.end();
});
module.exports = router;
and also the code in the client's JavaScript file:
window.onload = function () {
var valor = [];
var socket = io.connect('http://localhost:3000');
var content = document.getElementById("content");
socket.on('valor', function (data) {
valor = data;
var html = '';
for (var i = 0; i < valor.length; i++) {
html += '<b>' + (valor[i]) + ': </b><br>';
}
content.innerHTML = html;
});
}
To make it clearer, I also show you the code of my UNO.ejs page:
<html>
<head>
<title>UNO</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<script src='/javascripts/unoScript.js' type='text/javascript'></script>
<script src='/socket.io/socket.io.js' type='text/javascript'></script>
<body>
<div id='content'></div>
</body>
</html>