/    Sign up×
Community /Pin to ProfileBookmark

NodeJS : webserver with embedded socket and DOM update

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[/B] based on certain command received on the server socket.

[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]

[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’);
[/code]

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

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@simozzauthorAug 04.2017 — Hello,

I understand that cheerio does not parse its content to browser output.

What could I use for this task ?

Simon
Copy linkTweet thisAlerts:
@rootAug 04.2017 — Moved to JavaScript Frameworks (as Node.js is a framework / library)
Copy linkTweet thisAlerts:
@sagarchavadaMay 16.2019 — you must set you source file of the socket.io site in HTML file.

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-05-16/1557984228-907533-image.png]

and must see your hosting URL and Port .
×

Success!

Help @simozz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...