将网址传递给dns.lookup()

时间:2019-01-23 14:11:34

标签: node.js dns

我在dns.lookup()上获取网址错误。有帮助吗?举例来说 https://stackoverflow.com/questions/41942690/removing-http-or-http-and-www/41942787

我已经删除了https://,但仍然无法正常工作。

var dns = require('dns');
var url = "https://stackoverflow.com/questions/41942690/removing-http-or-http-and-www/41942787";
dns.lookup(url,(error,address,family)=>{
  console.log(error,address,family)
})

1 个答案:

答案 0 :(得分:0)

dns.lookup(hostname[, options], callback)

解析URL并检索主机名,然后将其传递给dns.lookup()函数。

const dns = require('dns');
const url = require('url'); 
const lookupUrl = "https://stackoverflow.com/questions/41942690/removing-http-or-http-and-www/41942787";

const parsedLookupUrl = url.parse(lookupUrl);

console.log(parsedLookupUrl);
  

网址身份验证:空哈希值:空主机:“ stackoverflow.com”主机名:   “ stackoverflow.com” href:   “ removing http:// or http:// and www”   路径:“ / questions / 41942690 / removing-http-or-http-and-www / 41942787”   路径名:“ / questions / 41942690 / removing-http-or-http-and-www / 41942787”   端口:null协议:“ https:”查询:null搜索:null斜杠:true   网址原型

dns.lookup(parsedLookupUrl.hostname,(error,address,family)=>{
  console.log(error,address,family)
});