繁体   English   中英

错误:发送 https 请求 Node.js 和 API 时出现 getaddrinfo ENOTFOUND

[英]Error: getaddrinfo ENOTFOUND when sending https request Node.js and API

我正在开发仪表板,我需要连接到 API 并捕获 Auth 令牌,然后使用 HTTPS 协议发送信息。 我使用 Nodejs,当我运行代码时,pm2 监视器上出现下一个错误:

 Error: getaddrinfo ENOTFOUND my.url.net/path at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) { errno: -3008, code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'my.url.net/path' }

这也是我提出请求的代码(Node.js):

const path = require('path');
require('dotenv').config({path: path.join('path','.env')});
const https     = require('https');
const database = require('./sql');
const fs         = require ('fs');

const user = process.env.USER;
const pwd  = PWD;
const host = 'https://my.url.net/extencio';
const host_1 = 'my.url.net/extention';

async function getLoginToken(pForce){

 if (login_token.Health && !pForce) { return login_token }

  //Creates the POST request
  const options = {
    protocol: 'https:',
    hostname: host_1,
    path: '/api/auth/token',
    method: 'POST',
    headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
        }
  };

  //Body of the POST request, contains the user and password
  const post_data = JSON.stringify({username: user, password: pwd});
.

这是代码的rest:

 return new Promise((resolve, reject) => { const req = new https.request(options, (response) => { response.setEncoding('utf8'); response.on('data', function(chunk){ const output = JSON.parse(chunk); if(output.token){ login_token.Health = true; login_token.Token = output.token; resolve(login_token) } else{ login_token.Health = false; login_token.Token = ''; resolve(login_token); } }); }); req.write(post_data); req.end(); req.on('error', function(err) { console.log(err); login_token.Health = false; login_token.Token = ''; resolve(login_token) }); }); }

似乎正在尝试获取完整的 url 的 addrinfo,而不仅仅是主机名。 我会将主机名仅作为“my.url.net”放入选项中,并使用 url 的 rest 更新路径。

@Eric0607 您提供的错误 stackoverflow.com/questions/65810720/... 不再显示,我可能来不及回复了。

但如果您遇到“无效的本地证书 SSL 错误”,我发现这是一个适用于它的修复程序。 禁用 SSL 签入您的代码,不推荐,但它会暂时起作用,完成后将其打开,否则可能会有风险。

去掉协议,只为主机使用域名。 例如:

错误的:

const host = 'https://my.url.net/extencio'
const path = '/api/auth/token'

正确的:

const host = 'my.url.net'
const path = '/extencio/api/auth/token'

请参阅http.request选项的文档

暂无
暂无

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

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