3

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)
    });
    
  });
}
Eric0607
  • 31
  • 1
  • 1
  • 5

3 Answers3

12

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.

Dmitry
  • 1,556
  • 1
  • 14
  • 26
0

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.

Simone-Cu
  • 1,109
  • 8
  • 21
  • @Eric0607 do you mean you have tried "protocol":"https", "hostname": "my.url.net", "path": "extention/api/auth/token" (I'm not sure that path is ok, but it's based from your setup) Please note also that protocol is not "https:" – Simone-Cu Jan 19 '21 at 08:27
  • Yes I mean the protocol https, "hostname" "my.url.net/extention", "path" "/api/auth/token" I need to use the https protocol because I have SSL certificates, also wen i made the change from http I get error that only https are spected: Error: getaddrinfo ENOTFOUND my.url.net/extention/api/auth/token at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) { errno: -3008, code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'my.url.net/extention/api/auth/token' } – Eric0607 Jan 19 '21 at 13:06
  • So no, you didn't read. "hostname": "my.url.net" and not "my.url.net/extention". Furthermore, you didn't read well the protocol you have written in the question: it should be "https" and not "https:" because it doesn't have the colons. – Simone-Cu Jan 19 '21 at 15:01
  • I made the changes as it say and now I have HOST: my.url.net Path: /Ext/api/auth/token but now I have this error https://stackoverflow.com/questions/65810720/error-self-signed-certificate-in-certificate-chain-node-js Any idea. Thank alot for the help – Eric0607 Jan 22 '21 at 17:22
0

@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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Tango
  • 1