Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * App: ivaoToJson
- * Description: Simple API REST to convert ivao whazzup.txt to json data
- * Framework: Express on NodeJs
- * Author: Riccardo Cosenza
- * Mail: riccardo.cosenza81@gmail.com
- * Github: https://github.com/Rickyc81/ivaoToJson.git
- */
- const express = require('express');
- const fetch = require('node-fetch');
- const helmet = require('helmet');
- const cors = require('cors');
- const getPilots = require('./getPilots');
- const app = express();
- const PORT = 3000;
- const URL = "http://api.ivao.aero/getdata/whazzup/whazzup.txt";
- app.use(helmet());
- app.use(cors());
- // Ivao Section: !GENERAL, !CLIENTS, !SERVERS, !AIRPORTS
- // Main page route
- app.get('/', (req, res) => {
- res.sendStatus(200);
- });
- app.get('/Clients', function(req, res) {
- let dataToSend = [];
- try {
- fetch(URL)
- .then(response => {
- console.log(response.ok)
- if (response) {
- return response.text();
- } else {
- return null
- }
- })
- .then(
- data => {
- if (data != null) {
- let splitted = data.split('\n');
- let startIndex = splitted.indexOf("!CLIENTS") + 1;
- let endIndex = splitted.indexOf("!AIRPORTS") + 2;
- let clients = splitted.slice(startIndex, endIndex);
- clients.forEach(line => {
- let fields = line.split(":");
- let temp = {}
- temp.callsign = fields[0];
- temp.vid = fields[1];
- temp.connectionTime = fields[37];
- temp.softwareName = fields[38];
- temp.softwareVersion = fields[39];
- temp.latitiude = fields[5];
- temp.longtitude = fields[6];
- temp.altitude = fields[7];
- temp.groundSpeed = fields[8];
- temp.heading = fields[45];
- temp.onGround = fields[46];
- temp.squawk = fields[17];
- temp.rating = fields[41];
- dataToSend.push(temp);
- })
- res.status(200).send({
- status: 'success',
- code: 200,
- response: {
- message: 'fetch data ok',
- data: dataToSend,
- },
- })
- } else {
- res.send("Data not found!")
- }
- })
- } catch (error) {
- res.status(400).send({
- status: 'failure',
- code: 400,
- response: {
- message: 'fetch error',
- data: err,
- },
- })
- }
- });
- // Online pilots route
- app.get('/pilots', (req, res) => {
- //res.sendStatus(200);
- res.send(getPilots());
- });
- // Page metar & Taf route
- app.get('/metar', (req, res) => {
- res.sendStatus(200);
- });
- // Unknow routes
- app.all('*', (req, res) => {
- res.sendStatus(404)
- })
- app.listen(PORT, () => console.log(`Server in ascolto sulla porta ${PORT}`));
Add Comment
Please, Sign In to add comment