使 .local 解析为 IP 地址和端口 (mdns)

Making .local resolve to IP address AND port (mdns)

我正在使用 multicast-dns 节点模块来尝试完成这项工作。

在浏览器中查找 custom.local 给我设置的控制台消息,但我无法看到我的实际服务器 运行(在 localhost:12345 上这样做,其中 12345 是一个动态数字)。我希望在访问 custom.local 时能够看到我的本地服务器。这可能吗?

这是一些代码:

mdns.on("query", query => {
  if (query.questions[0] && query.questions[0].name === "custom.local") {
    console.log(query);

    mdns.respond({
      answers: [
        {
          name: "custom.local",
          type: "SRV",
          data: {
            port: n.get("p"), // dynamic port
            weight: 0,
            priority: 10,
            target: ip // local IP
          }
        }, {
          name: "custom.local",
          type: "A",
          data: ip,
          ttl: 300
        }
      ]
    });
  }
});

编辑:我可以很好地连接到我的本地服务器,这不是问题。

引用 cfreak:

You can't put port numbers in DNS. DNS is only for looking up an IP by name. For your browser to see it by the name alone you need a proxy program in front of your service or you need to run the service itself on port 80. Port numbers really shouldn't be dynamic. You should specify it in the setup of your service.

这回答了我的问题并提供了后续步骤。谢谢!

更新:Figured out what I was trying to do。这是一些代码!


找到解决方案,呜呜呜!

我正在使用 this module,但稍微调整了源代码(只是因为我有动态端口,因为我喜欢它)。

/* jshint undef: true, unused: true, esversion: 6, node: true */



"use strict";



//
//  G E T
//  P A C K A G E S

import express from "express";
import http from "http";
import local from "./server/local";

const n = express();



n.get("/", (req, res) => {
  res.send("Welcome home");
});



//
//  L A U N C H

const server = http.createServer(n);

server.listen(0, () => {
  const port = server.address().port;
  local.add(port, "custom.local");
});

希望这对您也有帮助,未来的互联网搜索者! :D 不要让其他 SE 网站上的负面人士让您失望。 :虚拟拳击: