MooShoe Gaming
MooShoe Gaming

Reputation: 11

Unable to connect from a different network express.js

I'm experimenting with node.js and express.js.

When I try to connect to my web server from any computer in my network, it works, but then when I try to connect from outside network the connection times out.

var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res) {
    res.send("Hello World");
});
http.listen(3000, '0.0.0.0', function() {
    console.log("Listening on port 3000!");
});

Upvotes: 1

Views: 125

Answers (2)

Emile Bergeron
Emile Bergeron

Reputation: 17430

I just tested your code and I'm able to access the server from outside my local network by navigating to:

http://173.0.[my].[ip]:3000

So the code is correct. It could be that you need to open the port 3000 to the outside world. Here's how it can be accomplished.

Through your router admin interface

Here's mine for example:

Single port forwarding

Where 192.168.1.130 is the local IP of the PC I'm running the http server on.

Don't forget to click the Save settings button in that interface to apply the changes.

Using a tool like ngrok (mentioned by eddiezane)

Install ngrok through their website or without leaving the command prompt, with the ngrok node wrapper.

npm install ngrok -g

Start your http server and then run:

ngrok http 3000

Navigate to one of the url in front of Forwarding:

ngrok http forward port 3000

The free version is more for a quick test and less as a definitive way to expose a service in a production environnement since every time you restart ngrok, a new user-hostile url is given to you.

Other possible problems

  • It could also be that you need to add an exception to the firewall (if on windows).

Upvotes: 2

eddiezane
eddiezane

Reputation: 916

To add to Emile's answer, I would check out ngrok which is an awesome tool that generates you a publicly accessible URL for a port on your local machine.

Here's a good blog post on it my buddy wrote.

Upvotes: 1

Related Questions