简体   繁体   中英

How do I make node.js work with vagrant share?

I built a small chat using PHP and node.js (using the socket.io library)

Essentially I use node.js for the server of the chat and PHP to handle the actual webpages.

Vagrant has the option to share your HTTP server with users: https://www.vagrantup.com/docs/share/http.html

The HTTP server is running on port 80 and node.js is running on port 3000.

On the chat.php page, I have this line of code:

socket = io.connect("http://localhost:3000");

When I execute the vagrant share command, it provides a URL which you can provide to other people and they will be able to access the site.

So given that URL, I edit the line of code mentioned above to include that URL:

socket = io.connect("http://ugly-elk-1232.vagrantshare.com:3000");

and then I start SSH into vagrant and start node from there.

However it doesn't work. On the chat page I can see timeout errors when socket.io tries to access port 3000.

Here's the error I get in the console (in chrome):

GET http://ugly-elk-1232.vagrantshare.com:3000/socket.io/?EIO=3&transport=polling&t=Lcp9sZh net::ERR_CONNECTION_TIMED_OUT

(the URL is random and will change every time I run vagrant share, but I always update it on the chat page)

Here's what's in my vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

    config.vm.box = "scotch/box"
    config.vm.network "private_network", ip: "192.168.33.10"
    config.vm.hostname = "scotchbox"
    config.vm.synced_folder ".", "/var/www", :mount_options => ["dmode=777", "fmode=666"]
    config.vm.network "forwarded_port", guest: 3306, host: 3306

    # Optional NFS. Make sure to remove other synced_folder line too
    #config.vm.synced_folder ".", "/var/www", :nfs => { :mount_options => ["dmode=777","fmode=666"] }

end

Is there any way to make this work and allow port 3000 to be shared and not just port 80?

To be clear, the actual webpages are served perfectly fine. It's just that node is not accessible when I use vagrant share.

Edit:

I managed to somewhat solve the problem.

I use vagrant share --http 80 in one window and vagrant share --http 3000 in another.

Then I change the URL so that it connects to the node server that was shared on port 3000.

So the code then looks like this: socket = io.connect("http://abc123.vagrantshare.com");

Including :3000 (the port) in the URL stops it from working. (not sure why, but I don't think this is the issue).

The problem now is that socket.io now resorts to polling and no longer uses websockets. I tried to force it to use websockets, but it gives 400 bad request every time it tries. Polling isn't necessarily bad and it works, but I wanted it to use websockets since I need to test how the site will behave when it's actually up and websockets are what will be used in that case.

because of

socket = io.connect("http://localhost:3000");

you're listening only locally and the app works only from your VM (it does not even work if you try to access it from your host machine)

If you want to share the app directly from port 3000 you should be able to run vagrant share as

vagrant share --http 3000 

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