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.
You can run node. js server on a typical shared hosting with Linux, Apache and PHP (LAMP).
JS uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads.
Node. js is a JavaScript framework for writing server-side applications. In its simplest form it allows you to trigger small JavaScript programs from the command line without any browser involved. For example, assuming node is installed if you write a JavaScript program in a file called hello.
NodeJS is just a way for you to run JavaScript outside the browser. It can be used to run desktop app servers or really anything else that you want to do with JavaScript and the thing that we are going to do is actually create a web server using NodeJS.
Your node.js server is running on a port determined at the end of the script usually. Sometimes 3000. but can be anything. The correct way for others to access is as you say...
http://your.network.ip.address:port/
e.g.
http://192.168.0.3:3000
Check you have the correct port - and the IP address on the network - not the internet IP.
Otherwise, maybe the ports are being blocked by your router. Try using 8080 or 80 to get around this - otherwise re-configure your router.
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.
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 yourport
is forwarded to your ipv4 address.
On Windows Firewall, tick all on Node.js:Server-side JavaScript.
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 (127.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With