2

I'm using nodejs as a server for a videogame, and i want to try the multiplayer part but, I can not connect from outside my computer via localhost.

So, I used express before and this worked:
var app = express();
var serv = app.listen(8081, "127.0.0.1");
Above, the server is using localhost(127.0.0.1), but it can be changed to whatever IP I want. And is listening to port 8081.

The problem is, I'm no longer using express, only Nodejs. I'm handling the request, respond and handlers "manually". I researched a little on the documentation of express here:
http://expressjs.com/es/4x/api.html#app.use
But honestly, I did not understand how this function work.


This is my server.js:

// Import the necessary modules var http = require('http');

// Server object server = {};

// Start the http server server.httpServer = http.createServer(function(req, res){ /* Stuff */ }

// Start the server server.httpServer.listen(8081, function(){ console.log('The server is listening on port 8081'); });

Alvaro Lopez
  • 43
  • 1
  • 8
  • 1
    app.listen is just a short cut for `http.createServer(app).listen(...args)` so any arguments which worked with app.listen should work the same with httpServer.listen – generalhenry Apr 27 '18 at 14:24
  • Your `http.listen()` code is fine. If you are trying to connect to that server from another computer on your local network, then you just need to find out what the local IP address is for your computer and have the other computer connect to that. And, if you are running a local firewall (like is built into windows), you may need to allow access through that on the specified port. – jfriend00 Apr 27 '18 at 15:01
  • If you're trying to connect to your server from outside your LAN, then there's more to do because the IP address is not publicly available and your router has a firewall. See [Hosting node app on my computer](https://stackoverflow.com/questions/35055859/hosting-node-app-on-my-machine/35056150#35056150) for a general idea of what's required for access from outside your network. – jfriend00 Apr 27 '18 at 15:01
  • I tried `server.httpServer.listen(8081, "172.17.17.212");` and it worked wonderfully for what I was looking for. That's a random IP btw. – Alvaro Lopez Apr 28 '18 at 16:34

4 Answers4

2

http.server.listen accepts an IP address to bind to

See https://nodejs.org/api/net.html#serverlistenport-host-backlog-callback

// Import the necessary modules
var http = require('http');

// Server object
server = {};

// Start the http server
server.httpServer = http.createServer(function(req, res){
/* Stuff */
}

// Start the server
server.httpServer.listen(8081, '192.168.0.1', function(){
    console.log('The server is listening on port 8081');
});
mmomtchev
  • 2,497
  • 1
  • 8
  • 23
0

I am not sure of what you're trying to do, but as I understand, you try to reach your Node server from an external IP.

You can't do things just like that. You either need a server with a public reachable IP on which you start your Node server, or you launch your Node server within a local IP.

If you want to expose your localhost publicly, one simple solution could be to use tools such as ngrok. But please, be aware that is not considered safe and/or a best practice.

Mornor
  • 3,471
  • 8
  • 31
  • 69
  • I want to use my house network, so I have the IP and I can access it to start my Node.js server there. So... I used this before: var app = express(); var serv = app.listen(8081, "172.17.17.212"); btw, that is a random IP. – Alvaro Lopez Apr 27 '18 at 14:45
0

First of all you are missing a closing bracket

server.httpServer = http.createServer(function(req, res){
/* Stuff */
});

The node server will run on that machine using localhost. Then other machines can contact the server using either the hostname or local private IP (if on the same network). You can get your private IP from running the command ipconfig or ifconfig, depending on your system.

If you want to connect to it from outside the network then you will need to open the specific port (8081) so it is accessible via the internet. You can then connect to your external IP (You can see from ipchicken.com). It is possible you don't have a static IP address so you could either get one or use something like noip.com . Alternatively access it via a domain name if you register one.

Shaun Webb
  • 426
  • 5
  • 10
0
app.listen(PORT, HOST, () => {
    console.log(`[${HOST}:${PORT}] Server is running`)
})

will work as you want. https://expressjs.com/en/4x/api.html#app.listen

M. Emre Yalçın
  • 608
  • 1
  • 7
  • 10