Connecting to Node.js server in an azure virtual machine

I created a VM on Azure and defined a TCP endpoint. Then I created a Node.js TCP server like this:

var net = require('net');
var host = '127.0.0.1';
var port = 8000;
var times = 1;

net.createServer(function (socket) {
console.log('CONNECTED: ' + socket.remoteAddress + ':' + socket.remotePort);

socket.on('data', function (msg) {
    console.log("Message received is: " + msg);
    if (times == 1)
        socket.write("Server says HI");
    times++;
    executeStatement();
});

socket.on('close', function (data) {
    console.log('CLOSED: ' + socket.remoteAddress + ' ' + socket.remotePort);
});
}).listen(port, host);

console.log('System waiting at http://localhost:8000');

      

I can connect to a server on the same host, so two applications on different ports on the same computer can communicate bi-directionally. When I try to connect to the VM via the public IP and port number, it fails. I tried to add a firewall rule on the VM for the private port (8000), but that doesn't matter either. Please can someone give me some ideas and / or links to help solve this problem.

+3


source to share


2 answers


When you bind your HTTP server to 127.0.0.1, you only listen to incoming requests on the local loopback interface . This means that any requests coming from other devices, even if they are on the same network, will not be able to execute your service.



Omitting the IP address will cause the service to listen on any available IP addresses on the host machine - see the Node.js docs.Note that some hosting providers will require your application to listen on a specific IP address in order to be able to serve requests (i.e. Openshift ).

0


source


Somehow stating that host 127.0.0.1 is causing the problem. I removed it and made the server listen on only one port i.e. Listen on (port). After that, I was able to detect that the port was open, and then my program on the remote computer was able to communicate bi-directionally with the virtual machine.



+1


source







All Articles