Search code examples
node.jsvagrantlocalhostvirtualboxportforwarding

Connect to node js server running on vagrant machine


I have a simple node http server running on a vagrant VM. I would like to address to it with my browser on my local machine.

varhttp=require("http");

http.createServer(function(request,response){
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}).listen(3000);

This is my Vagrantfile:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "ubuntu/xenial64"
    config.vm.network "forwarded_port", guest: 80, host: 8080
    config.vm.provision "shell", path: "config.sh"
end

I can't figure out how to address it from my browser.

Whenever I do curl localhost:3000 in the vagrant VM, I get the Hello world message.

From my local machine I get This site can’t be reached whenever I try to open localhost:8080, as suggested in Vagrant forwarding.


Solution

  • Your forward config forwards 80, but your application listens on 3000. Fix that and it should work.