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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With