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,050 points
34 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
• 27,640 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,050 points
28 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
• 27,640 points
52 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,020 points
356 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,020 points
50 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
138 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
• 27,640 points
35 views
0 votes
1 answer