Connect to node js server running on vagrant machine

357
December 30, 2016, at 12:24 PM

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.

Answer 1

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

Answer 2

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

READ ALSO
How to update express/lib/application.js to match different app.js file?

How to update express/lib/application.js to match different app.js file?

MEAN noob hereI was following this tutorial, but npm start gives me the following error:

234
MathQuil API Inclusion

MathQuil API Inclusion

MathQuill seems to be particularly hard to include in our project, when we exactly follow the instructions off their website and we put:

281
RxJS - Wait until 5 events to occur before continuing

RxJS - Wait until 5 events to occur before continuing

I have a simple RxJS5 observable like so:

601