Skip to content

Conversation

@YaroslavKazeev
Copy link

No description provided.

@durw4rd durw4rd self-assigned this Aug 26, 2025
Copy link

@durw4rd durw4rd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work Yaroslav - it meets the requirements of the assignment, and it's clear you know how to create an Express server, including testing.
With that said, I'd recommend looking again into the response you get back from the API, specifically into the units the API uses and a way to control this. As it stands, your server is serving back questionable data.

// Add middleware to parse JSON data
const data = await response.json();
if (response.ok) {
const temperature = data.main.temp / 10;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API provides the temperature in different units depending on the instructions given via the request query params. The way you parse the response here will give a number that may seem accurate under certain scenarios, but it certainly won't be a correct temperature in the selected destination ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my bad, thanks. Kelvin degrees can be converted into Celsius by subtracting 273,15. That is another formula with Fahrenheit conversion, where there is division by a constant.

} else {
throw new Error(`Error: ${data.message}, Status: ${data.cod}`);
}
} catch (error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cleaner and likely more helpful with potential debugging if you had separate handling for a missing cityName vs. an incorrect API key (or other reasons for error).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line of code allows exactly what you've suggested for different scenarios:
Error: Invalid API key. Please see https://openweathermap.org/faq#error401 for more info., Status: 401
Error: city not found, Status: 404
app.js:26
While a generic response is sent to the user:
res
.status(404)
.json({ error: "City is not found or the API key is wrong" });
This approach was suggested to us by another teacher, Utku. He was concerned about security and did not want to send specific error messages to the client, as this could open the way for a hacker to attack. Instead, specific messages should be logged and analyzed on the server side, which is what is done here.

@YaroslavKazeev
Copy link
Author

Michal, thanks for your review. It never hurts to have an additional pair of eyes to be sure that everything is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants