gpt4 book ai didi

javascript - 错误 : getaddrinfo ENOTFOUND when sending https request Node. js 和 API

转载 作者:行者123 更新时间:2023-12-04 22:38:28 28 4
gpt4 key购买 nike

我正在开发一个仪表板,我需要连接到一个 API 并捕获一个 Auth Token,然后使用 HTTPS 协议(protocol)发送信息。我使用 Nodejs,当我运行我的代码时,我在 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'
}

这也是我发出请求的代码 (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});
.

下面是剩余的代码:

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)
});

});
}

最佳答案

移除协议(protocol),只为主机使用域名。例如:

错误:

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

正确:

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

参见 documentation http.request 选项。

关于javascript - 错误 : getaddrinfo ENOTFOUND when sending https request Node. js 和 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65775738/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com