Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access a node express app from vagrant host machine

I am new to networking and assigning ports and things of that nature. I have been using vagrant for some time and have never had any problems trying to get a test environment up and then accessing it through the host machine browser. The set up for this in my Vagrantfile is this:

# network stuff
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.hostname = "test-box-debian"

Now I am trying to learn a bit about node.js, and every tutorial says I can run npm start and indeed this works fine. I can call wget localhost:3000 (port 3000 being the default in express) and in return get the index.html default page from express.

However when I try and access `192.168.33.10:3000' from the host browser, it doesn't work. I can run netstat and get the following as a result:

sudo netstat -ltpn | grep 3000
tcp6       0      0 :::3000                 :::*                         LISTEN      17238/node  

I can see that something doesn't look right but I just don't know enough about ports and networking to know what is wrong and how to fix it.

like image 650
eignhpants Avatar asked Jul 22 '15 00:07

eignhpants


2 Answers

First, ensure your server is listening to the right IP and that you haven't bound the Express listener elsewhere:

.listen(3000), NOT .listen(3000, '127.0.0.1')

Alternatively, try binding the Express server to your private IP or to the wildcard IP and see if that resolves your connectivity issues:

// Wildcard (All IP's) binding
  .listen(3000, '0.0.0.0')

// Specific binding
  .listen(3000, '192.168.33.10')

Lastly, port 3000 may not be accessible from the host. If none of the above options in your server code work, try adding the following line to your Vagrantfile:

config.vm.network "forwarded_port", guest: 3000, host: 3000

like image 181
Alex Avatar answered Sep 21 '22 16:09

Alex


Make sure you don't have a firewall on your VM blocking the port:

sudo iptables -I INPUT -p tcp --dport 3000 -j ACCEPT

Found the answer over at https://stackoverflow.com/a/28474080/1772120.

like image 42
Josh Rickert Avatar answered Sep 21 '22 16:09

Josh Rickert