【发布时间】:2019-08-04 11:37:33
【问题描述】:
我需要在我的 NodeJS 程序中使用特定的 DNS 服务器发出同步 DNS 请求(我需要实际请求,无需查找)。如果可能的话,我想使用标准的 DNS 库。我的代码在交互模式下工作,但不是在实际脚本中。我对同步问题不太满意,所以我不完全理解 await 和东西的实际作用。
这是我的代码,目前在我的脚本中:
var readline = require('readline-sync');
async function sendm(req, srv) {
console.log("Will ask " + srv.getServers()[0] + " for " + req);
const addresses = await srv.resolve4(req);
console.log(addresses);
}
当用户输入以“s”开头的内容时执行该函数:
const { Resolver } = require('dns').promises;
const dns = new Resolver();
dns.setServers(["8.8.8.8"]);
// ...
var input = "";
while(input != "q") {
input = readline.question("Command: ");
if(/^s/g.test(input)) { // input starts with s
console.log("Input starts with s");
sendm("stackoverflow.com", dns);
}
else if(/^r/g.test(input))
console.log("input starts with r");
// ...
}
这是一个输出:
# nodejs debug.js
Command: s
Input starts with s
Will ask 8.8.8.8 for stackoverflow.com
Command: s
Input starts with s
Will ask 8.8.8.8 for stackoverflow.com
Command: s
Input starts with s
Will ask 8.8.8.8 for stackoverflow.com
Command: r
input starts with r
Command: r
input starts with r
Command: s
Input starts with s
Will ask 8.8.8.8 for stackoverflow.com
Command: q
[ '151.101.1.69', '151.101.65.69', '151.101.129.69', '151.101.193.69' ]
[ '151.101.1.69', '151.101.65.69', '151.101.129.69', '151.101.193.69' ]
[ '151.101.1.69', '151.101.65.69', '151.101.129.69', '151.101.193.69' ]
[ '151.101.1.69', '151.101.65.69', '151.101.129.69', '151.101.193.69' ]
我需要在程序仍在运行时处理 DNS 地址。我该怎么办?我尝试了在 DNS 库文档中找到的不同方法,但没有任何效果。你能帮帮我吗?
非常感谢!
【问题讨论】: