-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (72 loc) · 3.17 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const cors = require('cors')
const xml2js = require('xml2js');
const airportDiagrams = require("airport-diagrams")
const chartSupplements = require("chart-supplements")
const app = express();
const port = 9001;
// enables all CORS requests
app.use(cors())
// Airport demographic, ownership, geographic, runways info
// FAA 28-day NASR subscription by way of api.aeronautical.info
app.get(`/airport/:airportId`, async (req, res) => {
const url = `https://api.aeronautical.info/dev/?airport=${req.params.airportId}&include=demographic&include=ownership&include=geographic&include=runways`
const apiResponse = await fetch(url)
const data = await apiResponse.json()
res.status(200).json(data)
})
// METARs - aviationweather.gov
app.get(`/metar/:airportId`, async (req, res) => {
const url = `https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=K${req.params.airportId}&hoursBeforeNow=2&mostRecent=true`
const apiResponse = await fetch(url);
const xmlData = await apiResponse.text();
const parser = new xml2js.Parser()
parser.parseStringPromise(xmlData).then(function (result) {
res.status(200).json(result)
})
.catch(function (err) {
console.log("Failed", err)
})
})
// TAFs - aviationweather.gov
app.get(`/taf/:airportId`, async (req, res) => {
const url = `https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=tafs&requestType=retrieve&format=xml&stationString=K${req.params.airportId}&hoursBeforeNow=2&mostRecent=true`
const apiResponse = await fetch(url);
const xmlData = await apiResponse.text();
const parser = new xml2js.Parser()
parser.parseStringPromise(xmlData).then(function (result) {
res.status(200).json(result)
})
.catch(function (err) {
console.log("Failed", err)
})
})
// Airport Diagrams - NOT WORKING
// app.get('/airportDiagram/:airportId', async (req, res) => {
// try {
// const diagrams = await airportDiagrams.list(`K${req.params.airportId}`);
// res.status(200).json(diagrams);
// } catch (error) {
// console.error('Error fetching airport diagram:', error);
// res.status(500).json({ error: 'An error occurred while fetching airport diagram' })
// }
// })
// Chart Supplements - WORKING, but not well. Do not include.
// app.get(`/chartSupplement/:airportId`, (req, res) => {
// chartSupplements.list(`K${req.params.airportId}`)
// .then(result => {
// res.status(200).json(result)
// })
// })
// listen command
app.listen(port, () => {
console.log(`Server is running on port ${port}`)
})
// for reference
// this code will return XML wrapped as JSON (not properly parsed to JSON).
// app.get(`/metar/:airportId`, async (req, res) => {
// const url = `https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=K${req.params.airportId}&hoursBeforeNow=2&mostRecent=true`
// const apiResponse = await fetch(url)
// const data = await apiResponse.text()
// res.status(200).json(data)
// })