Last active
June 18, 2018 22:19
-
-
Save bitgord/e84186ddace7d3fdf5e0caac5f2ef4d6 to your computer and use it in GitHub Desktop.
Query the Blockchain.info API with Nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// require express to connect server | |
var express = require("express"); | |
var app = express(); | |
// require request to connect to blockchain.info api | |
var request = require("request"); | |
// request blockchain.info api in json and save api data in variables | |
request({ | |
url: "http://blockchain.info/stats?format=json", | |
json: true | |
}, function(error, response, body) { | |
btcPrice = body.market_price_usd; | |
btcBlocks = body.n_blocks_total; | |
}); | |
// home page display current bitcoin price | |
app.get("/", function(req, res) { | |
res.send("Blockchain.info Price: " + btcPrice) | |
}; | |
// /blocks page display current block height | |
app.get("/block", function(req, res) { | |
res.send("Blockchain.info current block height: " + btcBlocks) | |
}; | |
// listen on server | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment