Alvaro Lopez
Alvaro Lopez

Reputation: 43

How to use a network IP instead of localhost with nodejs without express?

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'); });

Upvotes: 2

Views: 12305

Answers (4)

Criminal_Affair_At_SO
Criminal_Affair_At_SO

Reputation: 3420

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');
});

Upvotes: 3

M. Emre Yalçın
M. Emre Yalçın

Reputation: 654

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

Upvotes: 0

Shaun Webb
Shaun Webb

Reputation: 426

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.

Upvotes: 0

Mornor
Mornor

Reputation: 3783

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.

Upvotes: 0

Related Questions