Once I develop a module to name an API with HTTPS I can get the code to work if the handed choices
are onerous set.
Module with choices:
module.exports = (host) => {
const choices = {
host,
technique: 'GET',
port: 443,
path: `/api/newest?access_key=${course of.env.TOKEN}`,
}
return new Promise((resolve, reject) => {
strive {
https
.get(choices, (res) => {
console.log(res.statusCode)
if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
res.on('information', (chunk) => {
console.log(chunk.toString('utf8'))
})
resolve(false)
} else {
resolve(false)
}
})
.on('error', (err) => {
console.error(err)
resolve(Error(err))
})
} catch (err) {
reject(Error(err))
}
})
}
once I take away the choices I can get a response:
module with out choices:
const testedURL = `https://foobar.com/api/newest?access_key=api-token`
module.exports = () => {
return new Promise((testedURL, reject) => {
strive {
https
.get(choices, (res) => {
console.log(res.statusCode)
if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
res.on('information', (chunk) => {
console.log(chunk.toString('utf8'))
})
resolve(false)
} else {
resolve(false)
}
})
.on('error', (err) => {
console.error(err)
resolve(Error(err))
})
} catch (err) {
reject(Error(err))
}
})
}
and this works:
module.exports = (host) => {
const choices = {
host,
technique: 'GET',
port: 443,
path: `/api/newest?access_key=${course of.env.TOKEN}`,
}
return new Promise((resolve, reject) => {
strive {
https
.get(`${choices.host}${choices.path}`, (res) => {
console.log(res.statusCode)
if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
res.on('information', (chunk) => {
console.log(chunk.toString('utf8'))
})
resolve(false)
} else {
resolve(false)
}
})
.on('error', (err) => {
console.error(err)
resolve(Error(err))
})
} catch (err) {
reject(Error(err))
}
})
}
pondering it was the best way I wrote the get
in HTTPS I rewrote it to duplicate the documentation:
module.exports = (host) => {
const choices = {
host,
technique: 'GET',
port: 443,
path: `/api/newest?access_key=${course of.env.TOKEN}`,
}
// console.log(choices)
return new Promise((resolve, reject) => {
// I can copy this from the terminal and get the API response within the browser:
// console.log(`${choices.host}${choices.path}`)
strive {
const req = https.request(choices, (res) => {
if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
res.on('information', (chunk) => {
const textChunk = chunk.toString('utf8')
console.log(textChunk)
})
resolve(Error('Standing code not right'))
}
})
req.on('error', (e) => {
console.error(e)
reject(Error(e))
})
} catch (e) {
reject(Error(e))
}
}
however I get the identical error response:
{
Error: join ECONNREFUSED X.X.X.X:443
at TCPConnectWrap.afterConnect [as oncomplete] (internet.js:1106:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'join',
handle: 'X.X.X.X',
port: 443
}
In my analysis it seems I am constructing it accurately:
- Https request not working in node.js
- HTTPS
- node.js Error: join ECONNREFUSED; response from server
I can confirm my choices match with the onerous coded URL. What am I doing flawed in my choices
that forestalls it from working but it surely works once I move it as ${choices.host}${choices.path}
?