Hello,
I am trying to build a simple web interface which should open a server socket, receive a command, and update a DOM.
While looking for some solutions, I decided to use NodeJS, implement a simple web-server with an server socket with port different of 8080.
For this purpose I built a simple test: update a div content [B]debugText
[code=html]
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<meta charset=”utf-8″>
<meta http-equiv=’cache-control’ content=’no-cache’>
<meta http-equiv=’expires’ content=’0′>
<meta http-equiv=’pragma’ content=’no-cache’>
<title>Dynamic web test</title>
</head>
<body>
<div id=”debug”>
<h5 id=”debugText”></h5>
</div>
</body>
</html>
[code]
var htmlFilePath = “../index.html”;
var http = require(‘http’);
var cheerio = require(‘cheerio’);
var $ = cheerio.load(htmlFilePath);
var fs = require(‘fs’);
http.createServer(function (req, res)
{
fs.readFile(htmlFilePath, function(err, data)
{
res.writeHead(200, {‘Content-Type’: ‘text/html’});
res.write(data);
res.end();
});
}).listen(80);
var net = require(‘net’);
var server = net.createServer(function(socket)
{
socket.write(‘# test serverrn’);
socket.on(‘data’, function (data)
{
if (data.toString() == ‘updatern’)
{
// here I should update DOM debugTxt
$(“#debugText”).text(“Updated”);
}
});
});
server.listen(8080, ‘0.0.0.0’);
However, things don’t work as expected.
How do I have to modify my source code to accomplish this task ?
Thank you in advance.
Regards,
Simon