2017-08-31 59 views
0

我试图把这种终点: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); 
} 

有呼叫终点,而不是仅仅串联的说法更清洁的方式吗?

+0

可以使用字符串模板:'let uri = \'https://blockchain.info/rawtx/$ {req.params.hash} \''' – tymeJV

回答

1

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

相关问题