简体   繁体   English

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

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

I am developing a dashboard, I need to conect to a API and catch a Auth Token and afther that send info by using a HTTPS protocol.我正在开发仪表板,我需要连接到 API 并捕获 Auth 令牌,然后使用 HTTPS 协议发送信息。 I use a Nodejs, and when I run my code I have the next error on the pm2 monit:我使用 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' }

Also here is my code where I made the request (Node.js):这也是我提出请求的代码(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});
. .

Here is the rest of the code:这是代码的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) }); }); }

It seems that is trying to getaddrinfo of the full url, instead of just the hostname.似乎正在尝试获取完整的 url 的 addrinfo,而不仅仅是主机名。 I would put hostname in option as only "my.url.net" and update path with the rest of the url.我会将主机名仅作为“my.url.net”放入选项中,并使用 url 的 rest 更新路径。

@Eric0607 the error stackoverflow.com/questions/65810720/… you've provided is not showing anymore, I might've been too late to reply. @Eric0607 您提供的错误 stackoverflow.com/questions/65810720/... 不再显示,我可能来不及回复了。

but in case you got " an invalid local cert SSL error ", here is a fix I found that works for it.但如果您遇到“无效的本地证书 SSL 错误”,我发现这是一个适用于它的修复程序。 disable SSL check in your code , not recommended but it would work temporarily, turn it on after you're done or it can be risky. 禁用 SSL 签入您的代码,不推荐,但它会暂时起作用,完成后将其打开,否则可能会有风险。

Remove protocol, and use domain names only for the host.去掉协议,只为主机使用域名。 For instance:例如:

Wrong:错误的:

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

Correct:正确的:

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

See documentation for the http.request options.请参阅http.request选项的文档

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

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