This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
73 lines (62 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var express = require("express")
var request = require('request')
var morgan = require('morgan')
var bodyParser = require('body-parser')
var compression = require('compression')
var app = express()
var TX_URI = new RegExp("^https://btc.blockr.io/api/v1/tx/raw/")
function allowCrossDomain(req, res, next) {
res.header('Access-Control-Allow-Origin', process.env.CORS_ORIGINS)
res.header('Access-Control-Allow-Methods', 'POST, GET')
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
next()
}
app.use(morgan())
app.use(allowCrossDomain)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded())
app.use(compression())
app.post('/', function(req, res){
request({
method: 'POST',
uri: req.query.url,
body: JSON.stringify({hex: req.body.hex}),
header: { "Content-Type": "application/json" }
}, function(err, response, body){
res.statusCode = response.statusCode
res.send(body);
})
});
app.get('/', function(req, res){
request({
method: 'GET',
uri: req.query.url
}, function(err, response, body){
res.statusCode = response.statusCode
// express strips out body for 304
if (res.statusCode === 200 && TX_URI.test(req.query.url)) {
body = stripTransactionData(body)
}
res.send(body);
})
});
function stripTransactionData(body) {
var response = JSON.parse(body)
var stripped = {
status: response.status,
data: []
}
stripped.data = response.data.map(function(d) {
return {
tx: {
hex: d.tx.hex,
blockhash: d.tx.blockhash,
blockHeight: d.tx.blockHeight,
blocktime: d.tx.blocktime,
confirmations: d.tx.confirmations
}
}
})
return JSON.stringify(stripped)
}
app.listen(process.env.PORT || 9009);