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');
});
-
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
4 Answers
Reset to default 3http.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