I have a pretty straight-forward question. I made a web game with NodeJS, and I can successfully play it by myself with multiple browser windows open side-by-side; however, I'd like to know if it's possible for other local machines to be able to access and play the game with me too.
I naively tried using this url: my-ip-address:8000
and it won't work.
This question is related to
javascript
networking
node.js
const express = require('express');
var app = express();
app.listen(Port Number, "Your IP Address");
// e.g.
app.listen(3000, "192.183.190.3");
You can get your IP Address by typing ipconfig
in cmd if your Windows user else you can use ifconfig
.
var http = require('http');
http.createServer(function (req, res) {
}).listen(80, '127.0.0.1');
console.log('Server running at http://127.0.0.1:80/');
This worked for me and I think this is the most basic solution which involves the least setup possible:
ipconfig
to get your ip address.
Note this ip address. It should be something like "192.168.1.2" which is the value to the right of IPv4 Address field as shown in below format:Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : ffff::ffff:ffff:ffff:ffad%14
IPv4 Address. . . . . . . . . . . : 192.168.1.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
npm start <IP obtained in step 1:3000>
e.g. npm start 192.168.1.2:3000
<your_ip:3000>
i.e. 192.168.1.2:3000
and you will see your website.Faced similar issue with my Angular Node Server(v6.10.3) which set up in WIndows 10.
http://localhost:4201
worked fine in localhost. But http://{ipaddress}:4201
not working in other machines in local network.
For this I updated the ng serve
like this
//Older ng serve in windows command Prompt
ng serve --host localhost --port 4201
//Updated ng serve
//ng serve --host {ipaddress} --port {portno}
ng serve --host 192.168.1.104 --port 4201
After doing this modification able to access my application in other machines in network bt calling this url
http://192.168.1.104:4201
//http://{ipaddress}:4201
I had the same question and solved the problem. In my case, the Windows Firewall (not the router) was blocking the V8 machine I/O on the hosting machine.
My guess is that "Evented I/O for V8 Javascript" is the I/O process that node.js communicates to outside world and we need to free it before it can send packets outside of the local computer. After enabling this program to communicate over Windows firewall, I could use any port numbers to listen.
By default node will run on every IP address exposed by the host on which it runs. You don't need to do anything special. You already knew the server runs on a particular port. You can prove this, by using that IP address on a browser on that machine:
http://my-ip-address:port
If that didn't work, you might have your IP address wrong.
After trying many solution and lot of research I did to the following to make sure my localhost is accessible from other machine in same network. I didn't start my server with IPAddress as parameter to listen method as suggested by others in this question. I did the following to make sure my local node js server is accessible from other machine on same local network. My node server is running in Windows 10 machine.
Please see the attached screenshot:
After these changes, I am able to access my localhost using http://IPAddress:Port/ Thanks.
The port is probably blocked by your local firewall or router. Hard to tell without details.
But there is a simple solution for which you don't have to mess with firewall rules, run node as a privileded process to serve on port 80, etc...
Check out Localtunnel. Its a great Ruby script/service, which allows you to make any local port available on the internet within seconds. It's certainly not useful for a production setup, but to try out a game with colleagues, it should work just fine!
One tip that nobody has mentioned yet is to remember to host the app on the LAN-accessible address 0.0.0.0 instead of the default localhost. Firewalls on Mac and Linux are less strict about this address compared to the default localhost address (172.0.0.1).
For example,
gatsby develop --host 0.0.0.0
yarn start --host 0.0.0.0
npm start --host 0.0.0.0
You can then access the address to connect to by entering ifconfig
or ipconfig
in the terminal. Then try one of the IP addresses on the left that does not end in .255
or .0
And Don't Forget To Change in Index.html Following Code :
<script src="http://192.168.1.4:8000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
var socket = io.connect('http://192.168.1.4:8000');
Good luck!
I had this problem. The solution was to allow node.js through the server's firewall.
If you are using a router then:
Replace server.listen(yourport, 'localhost');
with server.listen(yourport, 'your ipv4 address');
in my machine it is
server.listen(3000, '192.168.0.3');
Make sure your port is forwarded to your ipv4 address.
On Windows Firewall, tick all on Node.js:Server-side JavaScript.
Make sure the server is running, either by using 'npm start', or 'nodemon' in the command line window.
Source: Stackoverflow.com