-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (129 loc) · 3.72 KB
/
server.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
var Hapi = require('hapi');
var Joi = require("joi");
var axios = require("axios");
var helper = require('./js/extract');
var server = new Hapi.Server();
server.connection({
port: 3000,
host: '0.0.0.0'
});
// TODO load the data from external APIs and figure out the top zip codes by amounts
// Expose that data for the UI
// Maryland Data URL: https://opendata.maryland.gov/resource/3ycv-rxy9.json
server.register([{ register: require('inert') }], function (err) {
if (err) {
return console.error(err);
}
//========= Start of Files ====== //
// serve node modules
server.route({
method: 'GET',
path: '/node_modules/{param*}',
handler: {
directory: {
path: 'node_modules',
listing: false,
index: true
}
}
});
// serve assets
server.route({
method: 'GET',
path: '/assets/{param*}',
handler: {
directory: {
path: 'ui/assets/',
listing: false,
index: true
}
}
});
// serve components
server.route({
method: 'GET',
path: '/components/{param*}',
handler: {
directory: {
path: 'ui/components/',
listing: false,
index: true
}
}
});
// serve index
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
return reply.file('ui/index.html');
}
});
// serve app
server.route({
method: 'GET',
path: '/app.js',
handler: function (request, reply) {
return reply.file('ui/app.js');
}
});
// serve controllers
server.route({
method: 'GET',
path: '/controllers.js',
handler: function (request, reply) {
return reply.file('ui/controllers.js');
}
});
//========= End of Files ====== //
//========= Start of API ====== //
// Get Request to fetch initial data from OpenData Maryland
server.route({
method: "GET",
path: "/data",
handler: async (request, response) => {
var data = {};
data = await axios.get('https://opendata.maryland.gov/resource/gja3-vy5r.json?fiscal_year='+request.query.year+'&$limit=50000')
.then(response => {
return helper.spentByZipcode(response.data);
})
.catch(error => {
console.log(error);
return {};
});
response(data);
}
});
// Get Request for zipcode to latitude and longitude
server.route({
method: "GET",
path: "/nominatim",
handler: async (request, response) => {
var data = {};
data = await axios.get('https://nominatim.openstreetmap.org/search.php?country=USA&postalcode='+request.query.zipcode+'&polygon_geojson=1&format=jsonv2')
.then(response => {
if(response.data.length == 0){
return {};
}
return {
lat: response.data[0].lat,
lon: response.data[0].lon
}
})
.catch(error => {
console.log(error);
return {};
});
response(data);
}
});
//========= End of API ====== //
//Start the Hapi server
server.start(function (err) {
if (err) {
return console.error(err);
}
console.log('Server started');
});
});