node.js - 使用 https 我如何对未定义的状态代码进行条件化?
问题描述
尝试在模块中测试 HTTP 状态代码,但我的第一个响应始终是undefined
:
foo.js
const pass = require('./bar')
const test = pass('https://www.google.com/') // URL for testing will be API URL
console.log(`The return is: ${test}`)
bar.js
尝试1:
module.exports = (url) => {
https
.get(url, (res) => {
console.log(res.statusCode)
if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
console.log(`Results: ${res.statusCode}`)
return true
}
function waiting(status) {
if (typeof status !== 'undefined') {
console.log('run timeout')
setTimeout(waiting, 250)
} else {
console.log(`Results: ${status}`)
return true
}
}
})
.on('error', (e) => {
console.error(`Error ${e}`)
return false
})
}
尝试2:
module.exports = (url) => {
https
.get(url, (res) => {
console.log(res.statusCode)
function waiting(status) {
if (typeof status !== 'undefined') {
console.log('run timeout')
setTimeout(waiting, 250)
} else {
console.log(`Results: ${status}`)
return true
}
}
})
.on('error', (e) => {
console.error(`Error ${e}`)
return false
})
}
其他检测尝试undefined
:
if (typeof res.statusCode === 'number' && res.statusCode !== undefined && res.statusCode !== null) {
if (!res.statusCode) {
if (typeof res.statusCode !== 'undefined') {
console.log(`Results: ${res.statusCode}`)
if (res.statusCode.toString()[0] === '2') return true
return false
}
研究:
我究竟做错了什么?在我的模块中,如何检查之后的状态代码,undefined
以便我可以返回 atrue
或false
从实际数值返回?
解决方案
在您的两次尝试中,您在 bar.js 中导出的函数都没有返回任何内容。由于您正在调用异步函数 ( https.get
),因此您也需要导出的函数是异步的。您可以转换您的函数使用承诺并在调用方使用 async/await。例如
foo.js
const pass = require('./bar');
(async function() {
const test = await pass('https://www.google.com/'); // URL for testing will be API URL
console.log(`The return is: ${test}`);
})();
请注意 IFEE 以获取异步范围,按照:using await on global scope without async 关键字
bar.js
const https = require('https');
module.exports = (url) => {
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
console.log(res.statusCode)
if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
console.log(`Results: ${res.statusCode}`)
resolve(true)
} else {
resolve(false)
}
})
.on('error', (e) => {
console.error(`Error ${e}`)
resolve(false)
})
});
}
或者,您可以使用如下回调:
foo.js
const pass = require('./bar');
pass('https://www.google.com/', test => {
console.log(`The return is: ${test}`);
});
bar.js
const https = require('https');
module.exports = (url, callback) => {
https
.get(url, (res) => {
console.log(res.statusCode)
if (typeof res.statusCode !== 'undefined' && res.statusCode.toString()[0] === '2') {
console.log(`Results: ${res.statusCode}`)
callback(true)
} else {
callback(false)
}
})
.on('error', (e) => {
console.error(`Error ${e}`)
callback(false)
})
}
推荐阅读
- excel - 需要内部调试,运行时错误 1004 Excel VBA 搜索框筛选器
- javascript - 如何在 Highcharts 中将注释文本添加到 x 和 y 轴?
- r - merlin_error_check 中的 merlin 包错误(模型 = 模型,数据 = 数据,时间变量 = 时间变量,:结果必须是数字
- python - 探索性数据分析 - 有没有办法在 Python 中绘制与其类别(另一列)相对应的子类别(列)?
- java - 如何对 Spring @Scheduled 任务进行性能和负载测试
- c++ - C++中类的常量
- python - 无法导入延迟预测,我必须做什么才能通过错误?
- python - python-telegram-bot 中的错误
- sql - in 运算符不使用 OrientDB 中的子查询
- java - 出现错误 DeploymentException:来自服务器 [400] 的 HTTP 响应不允许 HTTP 升级到 WebSocket