Connect to another computer using IP address

2020-07-24 06:23发布

问题:

I installed NodeJS on my computer and ran some tests and all works fine on my machine. Now I want a friend, that is not in the same network, to connect to my computer, so that NodeJS can response to my friend's request. But I don't know, on which IP and port I have to listen to and I don't know, which IP I have to give my friend, that he can connect to my computer.

I already tried to get my public computer IP via command prompt and ipconfig, but all attempts to connect to this address failed. Also when my friend connects to my router's IP address.

What am I making wrong and how to do it correctly?

回答1:

You need to:

  • Open a port on your computer and router (for example 3000), setting up your router to route the open port to your computer.
  • Run your script to listen your computer ip or all network interfaces of the computer (0.0.0.0).
  • Then your friend will be able to connect on your router ip through the open port.

The precise configuration depend a lot of your computer/router.
To try you can use this basic node server script:

var http = require('http');

http.createServer(function(req, res){
    res.writeHead(200, {'content-type': 'text/plain'});
    res.end('It works');
}).listen(3000, '0.0.0.0');