What is the best practice for making a get request in node with arguments?

84 views Asked by At

I'm attempting to call this end point: https://blockchain.info/rawtx/$tx_hash in the following manner using the request-response module:

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);
}

Is there a cleaner way of calling an end point as opposed to just concatenating the argument?

1

There are 1 answers

0
Sello Mkantjwa On BEST ANSWER

I think this is clean enough. Your other option is .concat() but that's just slower. If you prefer templating and are in an es6 supported environment then you can do let uri = 'https://blockchain.info/rawtx/${hash}'; as suggested in the above comment. But like I said, I think the way you are doing it now is fit for purpose.