Eri*_*607 3 javascript ssl https node.js npm
我正在开发一个仪表板,我需要连接到 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'
}Run Code Online (Sandbox Code Playgroud)
这也是我发出请求的代码(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});
Run Code Online (Sandbox Code Playgroud)
。
这是其余的代码:
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)
});
});
}
Run Code Online (Sandbox Code Playgroud)
Dmi*_*try 12
删除协议,仅对主机使用域名。例如:
错误的:
const host = 'https://my.url.net/extencio'
const path = '/api/auth/token'
Run Code Online (Sandbox Code Playgroud)
正确的:
const host = 'my.url.net'
const path = '/extencio/api/auth/token'
Run Code Online (Sandbox Code Playgroud)
请参阅文档了解选项http.request。