最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: getaddrinfo ENOTFOUND when sending https request Node.js and API - Stack Overflow

programmeradmin1浏览0评论

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. I use a Nodejs, and when I run my code I have the next error on the pm2 monit:

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

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. I use a Nodejs, and when I run my code I have the next error on the pm2 monit:

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):

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:

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)
    });
    
  });
}
Share Improve this question edited Jan 18, 2021 at 16:19 Eric0607 asked Jan 18, 2021 at 13:46 Eric0607Eric0607 311 gold badge1 silver badge5 bronze badges 4
  • But you're not making any HTTP request anywhere in this code – Jeremy Thille Commented Jan 18, 2021 at 13:50
  • Sorry I mean HTTPS – Eric0607 Commented Jan 18, 2021 at 13:55
  • 1 But you're not making any HTTPS request anywhere in this code – Jeremy Thille Commented Jan 18, 2021 at 13:55
  • I already did the question update – Eric0607 Commented Jan 18, 2021 at 14:01
Add a comment  | 

3 Answers 3

Reset to default 14

Remove the 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.

It seems that is trying to getaddrinfo of the full url, instead of just the hostname. I would put hostname in option as only "my.url.net" and update path with the rest of the url.

@Eric0607 the error stackoverflow.com/questions/65810720/… you've provided is not showing anymore, I might've been too late to reply.

but in case you got "an invalid local cert SSL error", here is a fix I found that works for it. 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.

发布评论

评论列表(0)

  1. 暂无评论