How to send JSON object from npm module to browser

0 votes

How can I send the JSON object "data" returned by the npm module "blockchain.info" in the callback "onRequest". I want to write this data in the browser.

var http = require("http");
var blockchain = require('blockchain.info');
var blockexplorer = blockchain.blockexplorer;

var txAdress = 'b4b69abc03e4a801201e57ca57891002e5e756e85dde77a17deff0b107185a78';

blockexplorer.getTx(txAdress, function (err, data) {
   if (err) return console.error(err);
    console.log(data.size);  // works fine
});

var server = http.createServer(onRequest).listen(8888);

function onRequest(request, response) {
   response.writeHead(200, {"Content-Type": "text/html"});
   response.write('<h3>List of transactions</h3>');
   //response.write('<p>' + data.size + '</p>');  // data undefined!
   response.end();
}
Sep 20, 2018 in Blockchain by slayer
• 29,350 points
522 views

1 answer to this question.

0 votes

A simple solution for this would be to make the call in the request event handler:

function onRequest(request, response) {

    blockexplorer.getTx(txAdress, function (err, data) {
       if (err) return console.error(err);
       response.writeHead(200, {"Content-Type": "text/html"});
       response.write('<h3>List of transactions</h3>');
       response.write('<p>' + data.size + '</p>');
       response.end();
    });

}
answered Sep 20, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

How to receive JSON object via POST method?

You need to decode it with json_decode() like this $json ...READ MORE

answered Aug 28, 2018 in Blockchain by slayer
• 29,350 points
474 views
0 votes
1 answer

How do I send ether from an EOA to a smart contract?

You include ether to send in the ...READ MORE

answered Sep 27, 2018 in Blockchain by digger
• 26,740 points
1,356 views
0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,371 views
0 votes
1 answer

How is a request sent from an app to a smart contract?

Yes, the contract is distributed by every node ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 17,100 points
565 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,706 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,237 views
0 votes
1 answer

How to parse Json data from remote server?

Yes, you can do it use the ...READ MORE

answered Aug 17, 2018 in Blockchain by digger
• 26,740 points
658 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP