-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (33 loc) · 1.19 KB
/
server.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
'use strict'
const coinbase = require('./exchange/coinbase')
const binance = require('./exchange/binance')
const gemini = require('./exchange/gemini')
const util = require('./util/util')
const express = require('express')
const app = express()
const port = 4000
app.set("query parser", (queryString) => {
return new URLSearchParams(queryString);
});
app.get('/exchange-routing', (req, res) => {
if (req.query.has("amount")) {
(async () => {
Promise.all([coinbase.costToBuy(req.query.get("amount")), binance.costToBuy(req.query.get("amount")), gemini.costToBuy(req.query.get("amount"))]).then(results => {
const result = util.findCheapest(results)
if (result.ok) {
res.json(result.ok);
} else if (result.error) {
res.status(500).json({ error: result.error });
} else {
res.status(500).json({ error: "something went wrong" });
}
})
})()
}
else {
res.status(400).json({ error: "'amount' expected as query parameter" });
}
})
app.listen(port, () => {
console.log(`App listening on port ${port}`)
})