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

后端 未结 15 1625
旧时难觅i
旧时难觅i 2020-12-02 07:55

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,

相关标签:
15条回答
  • 2020-12-02 07:59

    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.

    1. Open "Windows Defender Firewall with Advanced Security"
    2. Select "Inbound Rules" in the left pane.
    3. In the list of available rules, "Node.js Server-side Javascript" has "Block the connection" radio checked. Change this to "Allow the connection".

    Please see the attached screenshot:

    After these changes, I am able to access my localhost using http://IPAddress:Port/ Thanks.

    0 讨论(0)
  • 2020-12-02 08:00

    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

    0 讨论(0)
  • 2020-12-02 08:02

    put this codes in your server.js :

    app.set('port', (80))
    app.listen(app.get('port'), () => {
    console.log('Node app is running on port', app.get('port'))
    })
    

    after that if you can't access app on network disable firewall like this :

    0 讨论(0)
  • 2020-12-02 08:03

    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.

    0 讨论(0)
  • 2020-12-02 08:03

    I had this problem. The solution was to allow node.js through the server's firewall.

    0 讨论(0)
  • 2020-12-02 08:05

    This worked for me and I think this is the most basic solution which involves the least setup possible:

    1. With your PC and other device connected to the same network , open cmd from your PC which you plan to set up as a server, and hit 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

    1. Start your node server like this : npm start <IP obtained in step 1:3000> e.g. npm start 192.168.1.2:3000
    2. Open browser of your other device and hit the url: <your_ip:3000> i.e. 192.168.1.2:3000 and you will see your website.
    0 讨论(0)
提交回复
热议问题