简体   繁体   中英

Running Node.js server on centos on 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. Ensure the box has binded to the right address as @frederic said.
  2. Make sure its not the firewall: sudo service iptables stop
  3. Make sure your host (your computer) can access the vagrant VM. 3a. If you bridged, use the internal IP of the VM 3b. If you port-forwarded into the VM ( config.vm.network "forwarded_port", guest: 80, host: 8080 ), use http://localhost:8080 from your host (your computer).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM