我正在开发一个仪表板,我需要连接到一个API和捕获身份验证令牌,然后发送信息使用HTTPS协议。我使用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)
});
});
}
发布于 2021-12-16 10:25:06
删除协议,仅对主机使用域名。例如:
错误:
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
选项,请参见documentation。
发布于 2021-01-19 00:24:46
它似乎正在尝试获取完整url的getaddrinfo,而不仅仅是主机名。我会在选项中仅将主机名设置为"my.url.net“,并使用url的其余部分更新路径。
发布于 2021-11-12 01:10:09
@Eric0607错误stackoverflow.com/questions/65810720/…您提供的内容不再显示,我可能来不及回复。
但是如果你得到了"an invalid local cert SSL error",这是我找到的一个可以解决它的修复。disable SSL check in your code,不推荐,但它会暂时工作,在你完成后打开它,否则可能会有风险。
https://stackoverflow.com/questions/65775738
复制相似问题