2019102152 김다빈

Sort coin info & save to database

1 +#Trading Service
2 +
3 +./config/dev.js 생성 필요
4 +module.exports = {
5 + mongoURI:{mongoURI}
6 +}
7 +
......
1 +if (process.env.NODE_ENV === 'production') {
2 + module.exports = require('./prod');
3 +} else {
4 + module.exports = require('./dev');
5 +}
...\ No newline at end of file ...\ No newline at end of file
1 +module.exports = {
2 + mongoURI:process.env.MONGO_URI
3 +}
...\ No newline at end of file ...\ No newline at end of file
1 const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); 1 const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));
2 -const url = 'https://api.upbit.com/v1/market/all'; 2 +const url1 = 'https://api.upbit.com/v1/market/all';
3 3
4 const options = { method: 'GET', headers: { Accept: 'application/json' } }; 4 const options = { method: 'GET', headers: { Accept: 'application/json' } };
5 const express=require('express'); 5 const express=require('express');
6 const app=express(); 6 const app=express();
7 +const {Coin}=require("./models/Coin");
8 +
9 +
10 +const mongoose=require('mongoose');
11 +const config=require('./config/key');
12 +const connect = mongoose.connect(config.mongoURI,{
13 + useNewUrlParser: true, useUnifiedTopology: true
14 + })
15 + .then(()=>console.log('디비연결 성공'))
16 + .catch((err)=>console.log(err));
17 +
7 18
8 async function get_marketName() { 19 async function get_marketName() {
9 var data=new Array(); 20 var data=new Array();
10 //전체 암호화폐 리스트 불러오기 21 //전체 암호화폐 리스트 불러오기
11 - let response= await fetch(url, options) 22 + let response= await fetch(url1, options)
12 .then(res => res.json()) 23 .then(res => res.json())
13 .then(json => { 24 .then(json => {
14 for(i in json){ 25 for(i in json){
...@@ -19,9 +30,9 @@ async function get_marketName() { ...@@ -19,9 +30,9 @@ async function get_marketName() {
19 } 30 }
20 async function get_marketInfo(name_list){ 31 async function get_marketInfo(name_list){
21 //각 암호화폐 정보 조회 32 //각 암호화폐 정보 조회
22 - const url=`https://api.upbit.com/v1/ticker/?markets=${name_list}`; 33 + const url2=`https://api.upbit.com/v1/ticker/?markets=${name_list}`;
23 var arr=new Array(); 34 var arr=new Array();
24 - let response2=await fetch(url,options) 35 + let response2=await fetch(url2,options)
25 .then(res=>res.json()) 36 .then(res=>res.json())
26 .then(json=>{ 37 .then(json=>{
27 for(i in json){ 38 for(i in json){
...@@ -30,12 +41,39 @@ async function get_marketInfo(name_list){ ...@@ -30,12 +41,39 @@ async function get_marketInfo(name_list){
30 } 41 }
31 } 42 }
32 }) 43 })
44 + return arr
45 +}
46 +async function sort_data(arr){
47 + arr.sort((a,b)=>{
48 + return b[1]-a[1];
49 + })
33 return arr; 50 return arr;
34 } 51 }
35 -app.get('/get_market',async (req,res)=>{ 52 +async function save_coin(arr){
53 + for(var i=0;i<10;i++){
54 + if(arr[i]){
55 + const coin=new Coin({
56 + tid:i+1,
57 + name:arr[i][0],
58 + acc_trade_price_24h:arr[i][1],
59 + current_price:arr[i][2]
60 + });
61 + await coin.save((err)=>{
62 + if(err){
63 + console.log(err)
64 + }
65 + })
66 + }
67 + }
68 + return true;
69 +}
70 +app.get('/get_market',async (req,res)=>{
36 var name_list=(await get_marketName()); 71 var name_list=(await get_marketName());
37 var market_info=(await get_marketInfo(name_list)); 72 var market_info=(await get_marketInfo(name_list));
38 - res.json(market_info); 73 + var sort_info=(await sort_data(market_info));
74 + console.log(sort_info);
75 + res.json(await save_coin(sort_info));
76 +
39 }) 77 })
40 app.listen(5000,()=>{ 78 app.listen(5000,()=>{
41 console.log('server') 79 console.log('server')
......
1 +const mongoose=require('mongoose');
2 +
3 +const coinSchema=mongoose.Schema({
4 + tid:{
5 + type:Number,
6 + },
7 + name:{
8 + type:String,
9 + required:true
10 + },
11 + acc_trade_price_24h:{
12 + type:Number,
13 + required:true
14 + },
15 + current_price:{
16 + type:Number,
17 + required:true
18 + }
19 +})
20 +
21 +const Coin=mongoose.model("Coin",coinSchema);
22 +module.exports={Coin};
...\ No newline at end of file ...\ No newline at end of file