node.js - 如何将对象从此处导出到另一个文件?
问题描述
解析html后需要导出和对象。这样我就可以在另一个文件中导入这个文件,并且 Obj 将与函数内部相同。
const https = require('https');
const url = "https://..."
https.get(url, (resp) => {
let data = '';
const Obj = {}
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
Obj.data = 'data' // how do I get the Obj with key and value from here and export it to another file?
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
module.exports = ???
解决方案
在异步函数(返回Promise
)中抛出 HTTP 请求并导出此函数。只需在您导入的另一个文件中调用此函数(使用await
or .then
)。
推荐阅读
- javascript - 合并和组合 2 个应用程序。[Reactjs,反应路由器]
- postgresql - Datagrip 中的“脚本到...”选项?
- sql - WHERE 子句对性能的影响
- ios - 检测用户何时将 AVPlayer 静音
- c++ - 我收到此错误:AddressSanitizer:negative-size-param: (size=-8)
- sql - 根据条件对后续条目进行分组
- authentication - Laravel 7登录在第一次登录时不起作用,但后来正在工作
- postgresql - PostgreSQL 中的 DATE ADD 函数
- javascript - 使用 react-stl-obj-viewer 组件时如何访问画布以获取屏幕截图
- c++ - Valgrind 在程序段错误发生之前很久就使用“调用的纯虚拟方法”中止