-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (46 loc) · 1.23 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
var request = require('request')
const express = require("express")
const buyEndPoint = "https://api.unocoin.com/api/trades/btc/buy"
const sellEndPoint = "https://api.unocoin.com/api/trades/btc/sell"
const port = 80
var getRate = function (endPoint, callback) {
request(endPoint, {json: true}, (error, res, body) => {
if(error) {
callback(error, 0)
return
}
if (res.statusCode == 200) {
if (endPoint == buyEndPoint) {
callback(null, parseInt(body["buying_price"]))
}
else {
callback(null, parseInt(body["selling_price"]))
}
} else {
console.log(`error occured: ${body}`)
callback(body['error']['message'])
}
})
}
var app = express()
app.get("/buy", function (req, res) {
getRate(buyEndPoint, function (err, rate) {
if (err == null) {
res.json({ "buy": rate })
} else {
res.status(500).send('Something went wrong!')
}
})
})
app.get("/sell", function (req, res) {
getRate(sellEndPoint, function (err, rate) {
if (err == null) {
res.json({ "sell": rate })
} else {
res.status(500).send('Something went wrong!')
}
})
})
app.listen(port, function () {
console.log(`listening on port: ${port} ...`)
})