最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use a network IP instead of localhost with nodejs without express? - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question asked Apr 27, 2018 at 14:18 Alvaro LopezAlvaro Lopez 431 gold badge1 silver badge8 bronze badges 4
  • 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 Commented Apr 27, 2018 at 14:24
  • Your http.listen() code is fine. If you are trying to connect to that server from another puter on your local network, then you just need to find out what the local IP address is for your puter and have the other puter 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 Commented Apr 27, 2018 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 puter for a general idea of what's required for access from outside your network. – jfriend00 Commented Apr 27, 2018 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 Commented Apr 28, 2018 at 16:34
Add a ment  | 

4 Answers 4

Reset to default 3

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

See https://nodejs/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');
});

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.

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 mand 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.). It is possible you don't have a static IP address so you could either get one or use something like noip. . Alternatively access it via a domain name if you register one.

app.listen(PORT, HOST, () => {
    console.log(`[${HOST}:${PORT}] Server is running`)
})

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

发布评论

评论列表(0)

  1. 暂无评论