app.js 1.92 KB
var express = require('express');
var app = express();
var fs = require('fs');
const fetch = require('node-fetch');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get('/price', function(req, res) {
    fs.readFile('coin_name.txt', 'utf8', function(err,data){
        arr = data.split(",");
       // console.log(arr);
        var responseList = new Array();
        function print_coin(){
            var promise = new Promise(function(resolve,reject){
                for(j=0;j<arr.length/10;j++){
                    for(i=j*10;i<(j+1)*10;i++){
                        (function(i){
                            setTimeout(function(){
                            const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1';
                            const options = {method: 'GET', headers: {Accept: 'application/json'}};
                            var coinJson = new Object();
                            fetch(url, options)
                            .then(res => res.json())
                            .then(json => {
                                coinJson.coin = arr[i];
                                coinJson.price = json[0].trade_price;
                                responseList.push(coinJson);
                                console.log(i)
                                if(responseList.length>116){
                                    resolve(responseList);
                                }
                            })
                            .catch(err => console.error('error:' + err));
                            },i*110);
                       })(i);
                    }
                }
                
            });
            return promise;
        } 
        print_coin().then(function(resList){
            res.send(resList);
        });   
    })  
})
var server = app.listen(8082);
console.log("Server Created..");