How to run node.js client in a browser

all

I am very new to node.js. I am trying to make a tcp server client using node.js. So far, so good. The script server may work fine. You can also run the client script.

But the problem is that I could only get the client to work with the terminal by typing the command (node ​​client.js).
The fact is that I would like to run it in a browser so that I can receive data received from displaying the server in the browser.

How to do it?

Please, help.

Kawin.

This is the client code. (I cannot remember who originally created this script. I copy and paste it from somewhere, but forget the bookmark from which I get the link. Sorry for not giving credit to the owner of this script.)

var net = require('net');

var HOST = '192.168.0.88';
var PORT = 8888;

var client = new net.Socket();
client.connect(PORT, HOST, function() {

    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    // Write a message to the socket as soon as the client is connected, the   server will receive it as message from the client 
    client.write('B2\r\n');
});

// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
    console.log('DATA: ' + data);
    // Close the client socket completely
    client.destroy();
});

// Add a 'close' event handler for the client socket
client.on('close', function() {
    console.log('Connection closed');
}); 

Thank.

+4
2

Node.js javascript. , , . , , , , TCP, WebSockets (, socket.io, ).

+7

, , .

, http.

var http=require('http');
var server=http.Server(function(req,res) {
    res.end('<p>hello world</p><script>alert("hello world")</script>');
});

server.listen(8080);

, URL- "localhost: 8080"

-4

All Articles