首页 > 解决方案 > 如何将对象从此处导出到另一个文件?

问题描述

解析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 = ???

标签: node.jsasynchronousexport

解决方案


在异步函数(返回Promise)中抛出 HTTP 请求并导出此函数。只需在您导入的另一个文件中调用此函数(使用awaitor .then)。


推荐阅读