Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

node.js - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...