I am trying to transfert data from nodejs to html using socket.io. First I tried to transfert text from nodejs to html and it works :
nodejs code :
app.post('/timer', function(req, res){
res.sendFile(__dirname + '/public/status.html');
io.emit('messageFromServer', "text");
});
html code :
<ul id="messagesList">
</ul>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var list = document.getElementById("messagesList");
socket.on('messageFromServer', function (data) {
var item = document.createElement('li');
item.innerHTML = data;
list.appendChild(item);
});
</script>
Text appears between ul id="messagesList" AND /ul .
Now I want to transfer variable from nodejs to html. The variable is emit from python and it works. But I can't write this variable into the html page and I don't know why.
Nodejs code : info is the variable from python.
app.post('/timer', function(req, res){
res.sendFile(__dirname + '/public/status.html');
var info = req.body;
io.emit('messageFromServer', info);
console.log(info)
});
When I launch the python code, the console.log(info) write : { time: '10' }
html code (the same):
<ul id="messagesList">
</ul>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var list = document.getElementById("messagesList");
socket.on('messageFromServer', function (data) {
var item = document.createElement('li');
item.innerHTML = data;
list.appendChild(item);
});
</script>
When I launch the python code, the html write : [objet, Object], but I want : " time 10 ". How can I adapt my code ? Thanks.
===============================
Now it works with these codes : Nodejs :
app.post('/timer', function(req, res){
res.sendFile(__dirname + '/public/status.html');
var info = req.body;
io.emit('messageFromServer', info);
console.log(info)
});
and html :
<ul id="messagesList">
</ul>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var list = document.getElementById("messagesList");
socket.on('messageFromServer', function (data) {
var item = document.createElement('li');
item.innerHTML = data.time;
list.appendChild(item);
});
</script>
Thanks for helping me.