【发布时间】: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
我正在尝试将 JSON 数据从 https://blockchain.info/ticker 加载到 Node 中,如下所示:const btc = require(https://blockchain.info/ticker) 显然,这不起作用。怎么可能做到这一点?
【问题讨论】:
标签: javascript json node.js http https
您不能传递require() 网址。它需要一个文件名。
如果您想从远程服务器加载一些 JSON,您可以使用 request 或 request-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);
});
【讨论】: