简体   繁体   中英

Unable to connect to Node.js in Vagrant

I am trying to set up a Node.js app using Vagrant. There is also a Rails app inside that very same Vagrant box, which works OK. Node is using port 3001, so here are the config settings in Vagrantfile:

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "puppetlabs/ubuntu-14.04-32-puppet"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, host: 4567, guest: 3000
  config.vm.network :forwarded_port, host: 5678, guest: 3001

So, I started Node in Vagrant box. This shows that inside the Vagrant box it responds to requests on port 3001:

vagrant@localhost:~$ wget 127.0.0.1:3001
--2014-12-24 06:37:40--  http://127.0.0.1:3001/
Connecting to 127.0.0.1:3001... connected.
HTTP request sent, awaiting response... 200 OK

The guest machine is listening to port 3001:

$ sudo netstat -ltpn | grep 3001
tcp        0      0 127.0.0.1:3001          0.0.0.0:*               LISTEN      1422/node

The host is listening to port 5678:

sudo netstat -ltpn | grep 5678
tcp        0      0 0.0.0.0:5678            0.0.0.0:*               LISTEN      30966/VBoxHeadless

And yet, I can't connect to the app from the host browser.

What can be the problem?

Updated: Output on vagrant up :

[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] -- 3000 => 4567 (adapter 1)
[default] -- 3001 => 5678 (adapter 1)

Got the answer from a colleague.

Turned out, the node server in vagrant was listening to localhost, while it should have been listening to 0.0.0.0:

so this line in www file:

var server = app.listen(app.get('port'), 'localhost', function() {

had to be changed to:

var server = app.listen(app.get('port'), '0.0.0.0', function() {

All's working now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM