rickyc81

Untitled

May 1st, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.  * App:           ivaoToJson
  4.  
  5.  * Description:   Simple API REST to convert ivao whazzup.txt to json data
  6.  * Framework:     Express on NodeJs
  7.  * Author:        Riccardo Cosenza
  8.  * Mail:          riccardo.cosenza81@gmail.com
  9.  * Github:        https://github.com/Rickyc81/ivaoToJson.git
  10.  
  11.  */
  12.  
  13. const express = require('express');
  14. const fetch = require('node-fetch');
  15. const helmet = require('helmet');
  16. const cors = require('cors');
  17. const getPilots = require('./getPilots');
  18.  
  19. const app = express();
  20. const PORT = 3000;
  21. const URL = "http://api.ivao.aero/getdata/whazzup/whazzup.txt";
  22.  
  23.  
  24. app.use(helmet());
  25. app.use(cors());
  26.  
  27. //  Ivao Section: !GENERAL, !CLIENTS, !SERVERS, !AIRPORTS
  28.  
  29.  
  30.  
  31. // Main page route
  32. app.get('/', (req, res) => {
  33.   res.sendStatus(200);
  34. });
  35.  
  36. app.get('/Clients', function(req, res) {
  37.   let dataToSend = [];
  38.  
  39.   try {
  40.     fetch(URL)
  41.       .then(response => {
  42.         console.log(response.ok)
  43.         if (response) {
  44.           return response.text();
  45.         } else {
  46.           return null
  47.         }
  48.       })
  49.       .then(
  50.         data => {
  51.           if (data != null) {
  52.             let splitted = data.split('\n');
  53.             let startIndex = splitted.indexOf("!CLIENTS") + 1;
  54.             let endIndex = splitted.indexOf("!AIRPORTS") + 2;
  55.             let clients = splitted.slice(startIndex, endIndex);
  56.             clients.forEach(line => {
  57.               let fields = line.split(":");
  58.               let temp = {}
  59.               temp.callsign = fields[0];
  60.               temp.vid = fields[1];
  61.               temp.connectionTime = fields[37];
  62.               temp.softwareName = fields[38];
  63.               temp.softwareVersion = fields[39];
  64.               temp.latitiude = fields[5];
  65.               temp.longtitude = fields[6];
  66.               temp.altitude = fields[7];
  67.               temp.groundSpeed = fields[8];
  68.               temp.heading = fields[45];
  69.               temp.onGround = fields[46];
  70.               temp.squawk = fields[17];
  71.               temp.rating = fields[41];
  72.               dataToSend.push(temp);
  73.             })
  74.             res.status(200).send({
  75.               status: 'success',
  76.               code: 200,
  77.               response: {
  78.                 message: 'fetch data ok',
  79.                 data: dataToSend,
  80.               },
  81.             })
  82.           } else {
  83.             res.send("Data not found!")
  84.           }
  85.         })
  86.   } catch (error) {
  87.     res.status(400).send({
  88.       status: 'failure',
  89.       code: 400,
  90.       response: {
  91.         message: 'fetch error',
  92.         data: err,
  93.       },
  94.     })
  95.   }
  96. });
  97.  
  98. // Online pilots route
  99. app.get('/pilots', (req, res) => {
  100.   //res.sendStatus(200);
  101.   res.send(getPilots());
  102. });
  103.  
  104. // Page metar & Taf route
  105. app.get('/metar', (req, res) => {
  106.   res.sendStatus(200);
  107. });
  108.  
  109. // Unknow routes
  110. app.all('*', (req, res) => {
  111.   res.sendStatus(404)
  112. })
  113.  
  114. app.listen(PORT, () => console.log(`Server in ascolto sulla porta ${PORT}`));
Add Comment
Please, Sign In to add comment