Running Node.js server on centos on vagrant

时间:2015-08-07 02:33:04

标签: node.js centos vagrant

I'd like to run Node.js server in virtual machine.

I executed vagrant up and node Server.js.
Then I accessed to http://192.168.33.10:1337/ from host machine browser.
But, the browser outputs ERR_EMPTY_RESPONSE.
How do I fix it?

Vagrantfile

Vagrant.configure(2) do |config|
  config.vm.box = "centos"
  config.vm.network "private_network", ip: "192.168.33.10"
end

Server.js

var http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(1337, "192.168.33.10");

console.log('server running');

1 个答案:

答案 0 :(得分:1)

  1. 确保该框已绑定到正确的地址,如@frederic所说。
  2. 确保它不是防火墙:sudo service iptables stop
  3. 确保您的主机(您的计算机)可以访问流浪的VM。 3A。如果桥接,请使用VM的内部IP 3B。如果您通过端口转发到VM(config.vm.network" forwarded_port",guest:80,host:8080),请使用主机(您的计算机)中的http://localhost:8080