Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could others, on a local network, access my NodeJS app while it's running on my machine?

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.

like image 224
theabraham Avatar asked Mar 30 '11 17:03

theabraham


People also ask

Can NodeJS run on shared hosting?

You can run node. js server on a typical shared hosting with Linux, Apache and PHP (LAMP).

How many simultaneous connections can node js handle?

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.

How node JS works on server-side?

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.

Does NodeJS runs on server?

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.


4 Answers

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.

like image 65
Billy Moon Avatar answered Oct 11 '22 13:10

Billy Moon


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.

  1. Go to windows button
  2. Search "Firewall"
  3. Choose "Allow programs to communicate through Firewall"
  4. Click Change Setup
  5. Tick all of "Evented I/O for V8 Javascript" OR "Node.js: Server-side Javascript"

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.

like image 45
Sungsoo Koh Avatar answered Oct 11 '22 13:10

Sungsoo Koh


If you are using a router then:

  1. Replace server.listen(yourport, 'localhost'); with server.listen(yourport, 'your ipv4 address');

    in my machine it is

     server.listen(3000, '192.168.0.3');
    
  2. Make sure yourport is forwarded to your ipv4 address.

  3. On Windows Firewall, tick all on Node.js:Server-side JavaScript.

like image 51
Alif Avatar answered Oct 11 '22 12:10

Alif


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

like image 28
Yatit Thakker Avatar answered Oct 11 '22 12:10

Yatit Thakker