2016-10-24 74 views
0

我正在使用vagrant在virtualbox上创建一台计算机。该机器只有全新安装的Ubuntu(更新)和节点。无法连接到Ubuntu上的nodejs xenial

节点server.js工程查找与我的系统中的安装。可以在Chrome上看到“Hello world”。 但是当我ssh进入我的机器并在服务器上使用节点server.js时,我在chrome上获得了ERR_CONNECTION_RESET。

这里是我的代码

Vagrantfile:

Vagrant.require_version ">= 1.8.6" 

    Vagrant.configure("2") do |config| 
     config.vm.box = "ubuntu/xenial64" 

     config.vm.network "forwarded_port", guest: 3000, host: 3000 
     config.vm.synced_folder ".", "/var/www/", :mount_options => ["dmode=777", "fmode=666"] 

     config.vm.provider "virtualbox" do |virtualbox| 
     virtualbox.name = "vagrant" 
     virtualbox.memory = 512 
     virtualbox.cpus = 1 
     virtualbox.gui = false 
     end 
    end 

server.js

const http = require('http'); 

const hostname = '127.0.0.1'; 
const port = 3000; 

const server = http.createServer((req, res) => { 
    res.statusCode = 200; 
    res.setHeader('Content-Type', 'text/plain'); 
    res.end('Hello World\n'); 
}); 

server.listen(port, hostname,() => { 
    console.log(`Server running at http://${hostname}:${port}/`); 
}); 

软件版本:

  • 窗口10家
  • 流浪1.8.6
  • 的virtualbox 5.1.8
  • ubuntu的xenial 64
  • 节点v6.9.1

回答

1

当内部流浪运行时,地址127.0.0.1不一定映射到 'localhost' 的。拆下主机名和你应该罚款:

server.listen(port,() => { 
    console.log(`Server listening ${port}/`); 
}); 

省略the hostname defaults to 0.0.0.0

+0

是的! Works找到http://127.0.0.1:3000/。谢谢 – Ruben