-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (69 loc) · 2.7 KB
/
server.js
File metadata and controls
74 lines (69 loc) · 2.7 KB
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
74
var express = require('express')
var path = require('path')
var request = require('request')
var cointicker = require('coin-ticker')
var app = express()
app.set('view engine', 'pug')
app.set('views', path.join(__dirname, 'source/templates'));
app.use(express.static(__dirname + '/static'))
app.get('/', function (req, res, next) {
try {
res.render('homepage', { title: 'Home' })
} catch (e) { next(e) }
})
app.get('/contact', function (req, res, next) {
try {
res.render('contact', { title: 'Contact' })
} catch (e) { next(e) }
});
app.get('/searching', function (req, res, next) {
var sym, coin, ref, exchanges, results
sym = '฿'
ref = req.query.search.toUpperCase()
coin = ref + '_BTC'
if(ref === '') ref = 'BTC'
if(ref === 'BTC') coin = 'BTC_USD'
if(ref === 'BTC' || ref == 'USD' || ref == 'USDT') sym = '$'
exchanges = ['bittrex','poloniex','bitfinex','kraken','okcoin','bitstamp','coinbase'], exchangesGot = [], exchangesPoll = [], results = []
coinPoll = ref
if(coinPoll === 'BTC') coinRef = 'USD'
else coinRef = 'BTC'
var url = 'https://www.cryptocompare.com/api/data/coinsnapshot/?fsym=' + coinPoll + '&tsym=' + coinRef
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
for(let i = 0, l = body.Data.Exchanges.length; i < l; i++) {
exchangesGot.push( body.Data.Exchanges[i].MARKET.toLowerCase() )
}
for (var z = 0; z < exchanges.length; z++) {
for(var j = 0; j < exchangesGot.length; j++) {
if(exchangesGot[j].indexOf(exchanges[z]) != -1) exchangesPoll.push(exchangesGot[j]);
}
}
for(let i = 0, l = exchanges.length; i < l; i++) {
results.push(cointicker(exchanges[i], coin))
}
Promise.all(results).then(successCallback).catch(failureCallback)
function successCallback(found) {
var html = ''
for(let i = 0, l = found.length; i < l; i++) {
if(sym === '$' && typeof found[i].last !== 'undefined') fprice = Number(found[i].last).toFixed(2)
else fprice = Number(found[i].last).toFixed(8)
if(found[i].exchange === undefined) html += ''
else html += '<div class="col-sm-4 col-centered"><img class="centered" src="/images/' + found[i].exchange + '-ic.png" alt="' + found[i].exchange + '"><p class="centered">' + found[i].exchange + '<br>' + sym + fprice + '</p></div>'
}
if(html === '') html += '<p class="centered">That is not a valid currency.</p>'
res.end(html)
}
function failureCallback(failure) {
res.end('Im sorry, your request could not be completed as typed. Please try again later.')
console.log(failure)
}
}
})
})
app.listen(process.env.PORT || 80, function () {
console.log( 'Listening on http://bitdiff:' + (process.env.PORT || 80) )
})