【问题标题】:Node.js require JSON from web address?Node.js 需要来自网址的 JSON?
【发布时间】:2018-10-09 11:24:00
【问题描述】:

我正在尝试将 JSON 数据从 https://blockchain.info/ticker 加载到 Node 中,如下所示:const btc = require(https://blockchain.info/ticker) 显然,这不起作用。怎么可能做到这一点?

【问题讨论】:

    标签: javascript json node.js http https


    【解决方案1】:

    您不能传递require() 网址。它需要一个文件名。

    如果您想从远程服务器加载一些 JSON,您可以使用 requestrequest-promise 包。加载将是异步的,因此您需要在适当的回调中使用结果。这是一个例子:

    const rp = require('request-promise');
    
    rp({json: true}, "https://blockchain.info/ticker").then(data => {
        // use data here
        console.log(data);
    }).catch(err => {
        // process error here
        console.log(err);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-29
      • 2017-01-03
      • 2011-11-02
      • 2021-11-03
      • 1970-01-01
      • 2011-07-10
      • 2019-03-26
      相关资源
      最近更新 更多