简体   繁体   English

使用 createServer() 时如何获取 node.js 服务器可用 IP

[英]How to get node.js server available IPs when using createServer()

When I was starting a simple server using:当我使用以下方法启动一个简单的服务器时:

http-server -S -C cert.pem

I was getting a list of available IPs and I could choose one to connect to:我得到了可用 IP 的列表,我可以选择一个连接到:

Starting up http-server, serving ./ through https
Available on:
  https://192.168.1.2:8080
  https://169.254.187.206:8080
  https://169.254.74.61:8080
  https://127.0.0.1:8080
Hit CTRL-C to stop the server

How can I get the same list when using createServer()?使用 createServer() 时如何获得相同的列表?

I tried this to get one address:我试过这个来获得一个地址:

var server = https.createServer(options, function(request, response) {
  var uri = url.parse(request.url).pathname, 
      filename = path.join(process.cwd(), uri);
  
  fs.exists(filename, function(exists) {
    if(!exists) {
      response.writeHead(404, { "Content-Type": "text/plain" });
      response.write("404 Not Found\n");
      response.end();
      return;
    }
 
    if (fs.statSync(filename).isDirectory()) 
      filename += '/index.html';
 
    fs.readFile(filename, "binary", function(err, file) {
      if(err) {        
        response.writeHead(500, {"Content-Type": "text/plain"});
        response.write(err + "\n");
        response.end();
        return;
      }
      
      var mimeType = mimeTypes[filename.split('.').pop()];
      
      if (!mimeType) {
        mimeType = 'text/plain';
      }
      
      response.writeHead(200, { "Content-Type": mimeType });
      response.write(file, "binary");
      response.end();
    });
  });
    
}).listen(parseInt(port, 10));
    
var ip = server.address();
    
console.log("\n\n Static file server running at:\n address: " + ip.address + "\n family: " + ip.family + "\n port: " + ip.port + "\n\n Hit CTRL+C to stop the server");

and I got:我得到了:

Static file server running at:
address: undefined
family: IPv6
port: 8080

Hit CTRL+C to stop the server

Today I spent one hour trying to figure out why I can no more connect via WIFI to my local server.今天我花了一个小时试图弄清楚为什么我不能再通过 WIFI 连接到我的本地服务器。 It turned out that somehow the address I was using https://192.168.1.2:8080 had changed to https://192.168.1.3:8080 without adding any devices...原来,不知何故,我使用https://192.168.1.2:8080的地址已更改为https://192.168.1.3:8080而不添加任何设备...

i don't have enougth reputation to comment so i put it here what i think could help you.我没有足够的声誉来发表评论,所以我把它放在这里我认为可以帮助你。

First of all, you should assign a static ip to your machine, you could either:首先,您应该将 static ip 分配给您的机器,您可以:

  • Define in your computer an IP instead of relying on the DHCP service of your router在您的计算机中定义 IP 而不是依赖路由器的 DHCP 服务
  • Configure your DHCP (probably in your routeur config) to always give you the same IP address (static DHCP)配置您的 DHCP(可能在您的 routeur 配置中)以始终为您提供相同的 IP 地址(静态 DHCP)

Then when you use listen() you can put your fixed ip as: listen(8080, 192.168.1.2)然后,当您使用 listen() 时,您可以将固定的 ip 设置为: listen(8080, 192.168.1.2)

You can get all the network interfaces that have an address using networkInterfaces() from the os module.您可以使用os模块中的networkInterfaces()获取所有具有地址的网络接口。

Documentation: https://nodejs.org/api/os.html#os_os_networkinterfaces文档: https://nodejs.org/api/os.html#os_os_networkinterfaces

If you check http-server 's source code , it's doing something like this:如果您检查http-server源代码,它会执行以下操作:

var os = require('os');
var ifaces = os.networkInterfaces();

Object.keys(ifaces).forEach(function (dev) {
  ifaces[dev].forEach(function (details) {
    if (details.family === 'IPv4') {
      console.log(details.address);
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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