Skip to content

Commit 0fb885f

Browse files
authored
feat: add static endpoints for weather proxy (#628)
1 parent 47ac582 commit 0fb885f

File tree

3 files changed

+235
-17
lines changed

3 files changed

+235
-17
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
{
2+
"new york": {
3+
"coord": { "lon": -74.0059, "lat": 40.7127 },
4+
"weather": [
5+
{
6+
"id": 804,
7+
"main": "Clouds",
8+
"description": "overcast clouds",
9+
"icon": "https://cdn.freecodecamp.org/weather-icons/04d.png"
10+
}
11+
],
12+
"base": "stations",
13+
"main": {
14+
"temp": 14.33,
15+
"feels_like": 13.2,
16+
"temp_min": 13.21,
17+
"temp_max": 15.54,
18+
"pressure": 1008,
19+
"humidity": 53,
20+
"sea_level": 1008,
21+
"grnd_level": 1007
22+
},
23+
"visibility": 10000,
24+
"wind": { "speed": 3.6, "deg": 40 },
25+
"clouds": { "all": 100 },
26+
"dt": 1732123426,
27+
"sys": {
28+
"type": 1,
29+
"id": 4610,
30+
"country": "US",
31+
"sunrise": 1732103363,
32+
"sunset": 1732138471
33+
},
34+
"timezone": -18000,
35+
"id": 5128581,
36+
"name": "New York",
37+
"cod": 200
38+
},
39+
"chicago": {
40+
"coord": { "lon": -87.6298, "lat": 41.8781 },
41+
"weather": [
42+
{
43+
"id": 802,
44+
"main": "Clouds",
45+
"description": "scattered clouds",
46+
"icon": "https://cdn.freecodecamp.org/weather-icons/03d.png"
47+
}
48+
],
49+
"base": "stations",
50+
"main": {
51+
"temp": 8.91,
52+
"feels_like": 4.91,
53+
"temp_min": 7.86,
54+
"temp_max": 9.44,
55+
"pressure": 1009,
56+
"humidity": 50,
57+
"sea_level": 1009,
58+
"grnd_level": 987
59+
},
60+
"visibility": 10000,
61+
"wind": { "speed": 9.39, "deg": 285, "gust": 12.52 },
62+
"clouds": { "all": 40 },
63+
"dt": 1732123645,
64+
"sys": {
65+
"type": 2,
66+
"id": 2010190,
67+
"country": "US",
68+
"sunrise": 1732106817,
69+
"sunset": 1732141557
70+
},
71+
"timezone": -21600,
72+
"id": 4887398,
73+
"name": "Chicago",
74+
"cod": 200
75+
},
76+
"los angeles": {
77+
"coord": { "lon": -118.2437, "lat": 34.0522 },
78+
"weather": [
79+
{
80+
"id": 800,
81+
"main": "Clear",
82+
"description": "clear sky",
83+
"icon": "https://cdn.freecodecamp.org/weather-icons/01d.png"
84+
}
85+
],
86+
"base": "stations",
87+
"main": {
88+
"temp": 17.24,
89+
"feels_like": 15.88,
90+
"temp_min": 14.84,
91+
"temp_max": 19.62,
92+
"pressure": 1023,
93+
"humidity": 33,
94+
"sea_level": 1023,
95+
"grnd_level": 1003
96+
},
97+
"visibility": 10000,
98+
"wind": { "speed": 2.57, "deg": 70 },
99+
"clouds": { "all": 0 },
100+
"dt": 1732123664,
101+
"sys": {
102+
"type": 2,
103+
"id": 2075946,
104+
"country": "US",
105+
"sunrise": 1732113063,
106+
"sunset": 1732150008
107+
},
108+
"timezone": -28800,
109+
"id": 5368361,
110+
"name": "Los Angeles",
111+
"cod": 200
112+
},
113+
"tokyo": {
114+
"coord": { "lon": 139.6917, "lat": 35.6895 },
115+
"weather": [
116+
{
117+
"id": 501,
118+
"main": "Rain",
119+
"description": "moderate rain",
120+
"icon": "https://cdn.freecodecamp.org/weather-icons/10n.png"
121+
}
122+
],
123+
"base": "stations",
124+
"main": {
125+
"temp": 8.71,
126+
"feels_like": 5.38,
127+
"temp_min": 8.08,
128+
"temp_max": 9.81,
129+
"pressure": 1015,
130+
"humidity": 92,
131+
"sea_level": 1015,
132+
"grnd_level": 1014
133+
},
134+
"visibility": 7000,
135+
"wind": { "speed": 6.69, "deg": 330 },
136+
"rain": { "1h": 2.05 },
137+
"clouds": { "all": 75 },
138+
"dt": 1732123711,
139+
"sys": {
140+
"type": 2,
141+
"id": 268395,
142+
"country": "JP",
143+
"sunrise": 1732137787,
144+
"sunset": 1732174284
145+
},
146+
"timezone": 32400,
147+
"id": 1850144,
148+
"name": "Tokyo",
149+
"cod": 200
150+
},
151+
"london": {
152+
"coord": { "lon": -0.1278, "lat": 51.5074 },
153+
"weather": [
154+
{
155+
"id": 800,
156+
"main": "Clear",
157+
"description": "clear sky",
158+
"icon": "https://cdn.freecodecamp.org/weather-icons/01n.png"
159+
}
160+
],
161+
"base": "stations",
162+
"main": {
163+
"temp": 2.62,
164+
"feels_like": 0.84,
165+
"temp_min": 1.72,
166+
"temp_max": 3.49,
167+
"pressure": 1010,
168+
"humidity": 81,
169+
"sea_level": 1010,
170+
"grnd_level": 1005
171+
},
172+
"visibility": 10000,
173+
"wind": { "speed": 1.79, "deg": 285, "gust": 3.13 },
174+
"clouds": { "all": 1 },
175+
"dt": 1732123462,
176+
"sys": {
177+
"type": 2,
178+
"id": 2075535,
179+
"country": "GB",
180+
"sunrise": 1732087658,
181+
"sunset": 1732118707
182+
},
183+
"timezone": 0,
184+
"id": 2643743,
185+
"name": "London",
186+
"cod": 200
187+
}
188+
}

apps/weather-proxy/server.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require('dotenv').config();
22
var request = require('request');
33
var path = require('path');
44
var imgLinks = require('./data/imgLinks.json');
5+
var cities = require('./data/cities.json');
56
var express = require('express');
67
var app = express();
78

@@ -49,6 +50,18 @@ app.get('/status/ping', (req, res) => {
4950

5051
app.use('/images', express.static('images'));
5152

53+
app.get('/api/city/:city', function (req, res) {
54+
const city = req.params.city.toLocaleLowerCase();
55+
56+
if (cities[city]) {
57+
res.status(200).json(cities[city]);
58+
} else {
59+
res.status(404).json({
60+
error: `Weather information for city '${city}' not found.`
61+
});
62+
}
63+
});
64+
5265
app.get('/api/current', function (req, res) {
5366
var longitude = req.query.lon;
5467
var latitude = req.query.lat;

apps/weather-proxy/views/index.html

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,44 @@
1717
background-color: #ff9;
1818
padding: 3px;
1919
}
20-
p.usage {
20+
21+
p.usage,
22+
p.examples {
2123
display: block;
2224
background-color: #d0e7d0;
2325
padding: 15px;
2426
border-radius: 15px;
2527
max-width: 900px;
2628
line-height: 1.5em;
27-
margin: 0 auto;
29+
margin: 0 auto 20px auto;
2830
}
2931
</style>
3032
</head>
3133
<body>
3234
<h2>freeCodeCamp Weather API Passthrough</h2>
3335
<p class="usage">
34-
USAGE:<br />
35-
Use the endpoint:
36-
<code>https://weather-proxy.freecodecamp.rocks/</code>. Use this endpoint
37-
to get the weather at a location. To prevent abuses this server accepts
38-
<code>GET</code> requests only, and serves only the route
39-
<code>/api/current?lon=:longitude&lat=:latitude</code>. Images links are
40-
included in the JSON under <code>weather[0].icon</code>. This is enough to
41-
complete the challenge.
42-
<br />
43-
Example:
44-
<br />
45-
Request:
36+
<strong>Usage:</strong><br />
37+
Use <code>https://weather-proxy.freecodecamp.rocks/</code> to get weather
38+
information for a location in JSON format. This server only accepts
39+
<code>GET</code> requests, and only serves the routes
40+
<code>/api/city/:city</code> and
41+
<code>/api/current?lon=:longitude&lat=:latitude</code>.
42+
</p>
43+
44+
<p class="examples">
45+
<strong>Example Requests:</strong><br />
46+
<a
47+
href="https://weather-proxy.freecodecamp.rocks/api/city/new york"
48+
target="_blank"
49+
><code
50+
>https://weather-proxy.freecodecamp.rocks/api/city/new york</code
51+
></a
52+
><br />
53+
This endpoint does not return the current weather and the only available
54+
cities for this endpoint are <code>New York</code>, <code>Chicago</code>,
55+
<code>Los Angeles</code>, <code>Tokyo</code>, and
56+
<code>London</code>.<br />
57+
To get the current weather for any location use:<br />
4658
<a
4759
href="https://weather-proxy.freecodecamp.rocks/api/current?lat=35&lon=139"
4860
target="_blank"
@@ -51,7 +63,10 @@ <h2>freeCodeCamp Weather API Passthrough</h2>
5163
></a
5264
>
5365
<br />
54-
Response:
66+
</p>
67+
68+
<p class="examples">
69+
<strong>Example Response:</strong><br />
5570
<code>
5671
{ "coord": { "lon": 139, "lat": 35 }, "weather": [ { "id": 801, "main":
5772
"Clouds", "description": "few clouds", "icon":
@@ -61,8 +76,10 @@ <h2>freeCodeCamp Weather API Passthrough</h2>
6176
"wind": { "speed": 1.34, "deg": 177, "gust": 2.68 }, "clouds": { "all":
6277
16 }, "dt": 1597922946, "sys": { "type": 3, "id": 2019346, "country":
6378
"JP", "sunrise": 1597867673, "sunset": 1597915636 }, "timezone": 32400,
64-
"id": 1851632, "name": "Shuzenji", "cod": 200 }
65-
</code>
79+
"id": 1851632, "name": "Shuzenji", "cod": 200 } </code
80+
><br />
81+
Images links are included in the JSON under <code>weather[0].icon</code>.
82+
This is enough to complete the challenge.
6683
</p>
6784
<script>
6885
(function (i, s, o, g, r, a, m) {

0 commit comments

Comments
 (0)