TypeError [ERR_INVALID_OPT_VALUE]:对选项“主机名”无效

时间:2019-05-31 01:24:44

标签: node.js http dns

我收到此错误:

unhandledRejection: TypeError [ERR_INVALID_OPT_VALUE]: The value "blue-test.cle1x.us-west-1.rds.amazonaws.com" is invalid for option "hostname"
    at Object.lookupService (dns.js:173:11)
    at /app/main.js:32:7
    at new Promise (<anonymous>)
    at getDNSInfo (/app/main.js:31:36)
    at /app/main.js:113:12
    at Array.map (<anonymous>)
    at Server.<anonymous> (/app/main.js:112:68)
    at Server.emit (events.js:200:13)
    at parserOnIncoming (_http_server.js:709:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:116:17)

我使用了这个电话:

  const dns = require('dns');
  dns.lookupService(host, port, (err, hostname, service) => {
    console.log('dns resolved:', {hostname, service, err});
    resolve({err, hostname, service});
  });

并且主机名作为主机传递,所以有人知道这是怎么回事吗?

1 个答案:

答案 0 :(得分:1)

dns.lookupService()的第一个参数是address,而不是host。根据Node.js document

  

dns.lookupService(地址,端口,回调)

     

使用操作系统的基础getnameinfo实现将给定的地址和端口解析为主机名和服务。

如果要将blue-test.cle1x.us-west-1.rds.amazonaws.com解析为其IP地址,dns.lookup()是正确的选择。