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,240 points
44 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,600 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,240 points
35 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,600 points
74 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,090 points
485 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,090 points
63 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,810 points
182 views
0 votes
1 answer
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,600 points
51 views
0 votes
1 answer