-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (45 loc) · 1.47 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
var express = require('express');
var app = express();
const request = require('request');
const fs = require('fs');
const path = require('path');
app.get('/getTradeItem/:tradeId', function (req, res) {
const dir = path.join(__dirname, "/test.json")
if (!fs.existsSync(dir)) {
request('https://api.bitcoiva.com/markets', function (error, response, dataBody) {
if (error) res.send(error).status(500)
else {
fs.appendFile(dir, dataBody, function (err) {
if (err) throw err;
else {
let temp = readFile(req.params.tradeId)
res.send(temp).status(200)
}
})
}
});
} else {
let temp = readFile(req.params.tradeId)
res.send(temp).status(200)
}
});
app.get('/getTradeList', function (req, res) {
request('https://api.bitcoiva.com/markets', function (error, response, dataBody) {
if (error) res.send(error).status(500)
else {
dataBody = JSON.parse(dataBody)
res.send(dataBody).status(200)
}
});
});
function readFile(tradeId) {
const dir = path.join(__dirname, "/test.json")
let fileResponse = fs.readFileSync(dir, 'utf8');
fileResponse = JSON.parse(fileResponse)
let record = fileResponse.markets.filter((element) => tradeId == element.trading_pairs)
return record[0] ? record[0] : {};
}
app.listen(3000, (err) => {
if (err) console.log(err)
else console.log("server connected")
});