Connect to node js server running on vagrant machine

3k views Asked by At

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.

2

There are 2 answers

0
erik258 On BEST ANSWER

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

1
therobinkim On

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