在带有参数的节点中发出get请求的最佳做法是什么?

时间:2017-08-31 17:57:30

标签: node.js request-promise

我正在尝试使用请求 - 响应模块以下列方式调用此终点:https://blockchain.info/rawtx/$tx_hash

api.get('/transaction/:hash', (req, res) => {

    const hash = (req.params.hash);
    let uri = 'https://blockchain.info/rawtx/' + hash;

    rp(uri).then(function (txInfo) {
                let result = JSON.parse(txInfo);
}

是否有更简洁的方式来调用终点而不是仅仅连接参数?

1 个答案:

答案 0 :(得分:1)

我认为这很干净。您的另一个选择是.concat(),但这只是速度较慢。如果您更喜欢模板并且在es6支持的环境中,那么您可以让uri = 'https://blockchain.info/rawtx/${hash}';正如上述评论所述。但就像我说的那样,我认为你现在这样做的方式是合适的。

相关问题