1
Hello, I am using Socket i.o and when I submit a POST /Enviatemperatura, it is not automatically updating on my website. What I’m doing wrong?
Node.js
app.post('/EnviaTemperatura', function(req, res){
temperatura = req.body.temp;
console.log(temperatura);
res.send('Temperatura: ' + temperatura);
});
io.on("connection", function(socket) {
socket.emit('RecebTemp', temperatura);
});
HTML
<div class="col-xs-9 text-right">
<div class="huge"><span id="EnviaTemp">0</span> ºC</div>
<div>Temperatura no Interior da casa</div>
</div>
<script>
var socket = io();
socket.on('RecebTemp', function (temperatura) {
document.getElementById("EnviaTemp").innerHTML = temperatura;
});
</script>
In my case, it’s only updating when I give a refresh on the page. But I didn’t want to refresh the whole page, only in this DIV, since I have other elements on the screen that take a while to load.
What could be?
As your code currently stands, Emit is not running at the moment a POST request is made. Emit has to be within the app.post function. Of course, for that, you’ll need to adapt your code.
– Thiago Yoithi
I will leave an answer to supplement this comment.
– Thiago Yoithi