51

I'm currently working on a project and I would like to test it from another computer or phone. This computer is connected on the same network.

How can I connect to http://localhost:3000?

I'm using expressjs (nodejs module ) as server.

roschach
  • 8,390
  • 14
  • 74
  • 124
elreeda
  • 4,525
  • 2
  • 18
  • 45
  • 1
    Try using the IP Address? Depending on how your network is set up the computer name may work also. – TZHX Jun 08 '15 at 14:41
  • This seems to be a [pretty heavily duplicated](https://www.google.co.uk/search?q=site%3AStackoverflow.com+connect+to+localhost+from+another+computer&oq=site%3AStackoverflow.com+connect+to+localhost+from+another+computer+&aqs=chrome..69i57j69i58.5946j0j7&sourceid=chrome&es_sm=91&ie=UTF-8) question. – Quentin Jun 08 '15 at 14:43
  • 7
    Not duplcated he use xamp server but im not use it, i use `expressjs` – elreeda Jun 08 '15 at 14:44
  • The principles are exactly the same. You have a server listening. You connect to it from a client. It doesn't matter what software that server is (so long as you haven't configured it to **only** listen on the loopback interface, but you haven't shown us enough information to tell about that). – Quentin Jun 08 '15 at 14:45
  • @Quentin , when i get my ip adress 192.168.1.x and i access to thos adress from external device it not work ! ( i need to access on port 3000 ) – elreeda Jun 08 '15 at 14:48
  • @Reda — Well, yes. If you need port 3000 then you need port 3000. – Quentin Jun 08 '15 at 14:59
  • What, precisely, doesn't work? Is your Node app configured to listen on interfaces other than 127.0.0.1? – Quentin Jun 08 '15 at 15:02
  • tht what im looking for :) ! – elreeda Jun 08 '15 at 15:04
  • 9
    In support for this question... Changing `127.0.0.1` to `0.0.0.0` fixed my issue. I didn't get this info on any other "duplicate" questions. – Weishi Z Feb 19 '16 at 11:25
  • A very helpful explanation about 0.0.0.0: https://stackoverflow.com/a/20778887/2909851 – themefield Jan 03 '18 at 04:30

3 Answers3

96

Configure your application to run on 0.0.0.0 instead of 127.0.0.0(localhost). For example:

app.listen(3000, '0.0.0.0', function() {
    console.log('Listening to port:  ' + 3000);
});

Then from another computer, connect to 192.168.1.11:3000 (or whatever your local IP address is).

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
Bidhan
  • 10,607
  • 3
  • 39
  • 50
  • This answered its look logic, but it not work for me :3' ! , when i access to 0.0.0.0:3000, **Unable to connect** – elreeda Jun 08 '15 at 15:15
  • 7
    You need to access `192.168.1.11:3000`(or whatever your address is), **not** `0.0.0.0:3000`. – Bidhan Jun 08 '15 at 15:16
  • 3
    Is changing `127.0.0.1` to `0.0.0.0` a general practice for any server?? Or is it just for node.js?? – Weishi Z Feb 19 '16 at 11:22
  • 4
    Thank, it works! Can somebody explain why? – Luckylooke May 17 '16 at 11:46
  • I'm sorry but this is not working. I have changed my application to listen to 0.0.0.0 and trying to access my application from another computer using this url `192.168.1.11:3000.` but this is not working. It keeps on showing this site can't be reached and btw I'm using reactjs – Gardezi Jul 24 '17 at 08:41
  • 11
    0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown, or non-applicable target (a ‘no particular address’ place holder). In the context of a route entry, it usually means the default route. In the context of servers, 0.0.0.0 means all IPv4 addresses on the local machine. If a host has two IP addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs. @Luckylooke For more info you can see: https://www.howtogeek.com/225487/what-is-the-difference-between-127.0.0.1-and-0.0.0.0/ – Adi Azarya Aug 17 '17 at 16:35
  • @Bidhan it works between the devices which are in the same wifi. but, how can i connect to server i m running on my computer through web? – Wokers Dec 03 '20 at 21:45
  • @Workers You will need to expose your local server to the web. You can use something like ngrok for this. – Bidhan Dec 04 '20 at 16:48
16

your url should look like

http://yourcomputername:3000

to get computer name open command prompt windows and type hostname then hit enter

Yuri
  • 2,820
  • 4
  • 28
  • 40
  • 2
    Yes, this is what finally worked for me on macOS Sierra. I had been doing `http://.local:3000` but it is JUST the computer name: `http://doug:3000` that worked for me – Doug Mead Jul 07 '17 at 05:32
  • 2
    @Yuri's approach also works on Mac, by opening Terminal and typing `hostname` – ice cream Mar 26 '20 at 00:32
15

Given that the port is bind to any IP address other than 127.0.0.1 (localhost), you can access it from any other system.

To view your IP addresses, use ipconfig (Windows) or ifconfig (Linux) command. Find out the IP which is in the same network as the "other system" from which you want access. Then access it like, for example: 172.16.0.12:3000.

PS: Remember to include the port 3000 even when accessing it through another system. Also, hostnames may be used in place of IP addresses, if configured.

skrtbhtngr
  • 2,223
  • 23
  • 29