Skip to content

Conversation

@dashaaaa21
Copy link

No description provided.

@durw4rd durw4rd self-assigned this Nov 11, 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.

Good job, Daryna! You've used several coding patterns that went beyond what was asked in the instructions and made your application more robust; great that you explore these concepts even when not explicitly required in the exercise instructions!

Copy link

Choose a reason for hiding this comment

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

I know the project instructions don't mention this, but as a note for future projects, avoid committing API secrets to GitHub repositories. It's better to store them as environment variables, for example.

});

app.post("/weather", async (req, res) => {
const { cityName } = req.body ?? {};
Copy link

Choose a reason for hiding this comment

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

Using the nullish coalescing is a great practice for defensive programming, but note that the express.json() middleware, the res.body will always be at least an empty object, so it's not necessary here.


app.post("/weather", async (req, res) => {
const { cityName } = req.body ?? {};
if (!cityName || typeof cityName !== "string" || !cityName.trim()) {
Copy link

Choose a reason for hiding this comment

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

Well done with the thorough input validation!

return res.status(400).json({ weatherText: "Please provide a cityName." });
}

const url = `https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(
Copy link

Choose a reason for hiding this comment

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

Great that you are using encodeURIComponent to properly encode the city name in the URL!

)}&units=metric&appid=${keys.API_KEY}`;

try {
const r = await fetch(url);
Copy link

Choose a reason for hiding this comment

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

Nit: A more descriptive name of the variable would be handy.

const r = await fetch(url);
const data = await r.json();

if (!r.ok || String(data.cod) !== "200") {
Copy link

Choose a reason for hiding this comment

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

Great that you're checking the value of the data.cod value; I don't think I've seen anyone do this before!
I'm not that familiar with the API, but it seems a bit of a leap from the code not being '200' to concluding that it's a 404, city not found response, though. A more robust approach would be reading the cod value and returning the actual code the API provided.

Comment on lines +29 to +30
const name = data?.name ?? cityName;
const temp = data?.main?.temp;
Copy link

Choose a reason for hiding this comment

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

Good use of the optional chaining for safe property access 👍

const request = supertest(app);

describe("POST /weather", () => {
it("Quick test", () => expect(1).toBe(1));
Copy link

Choose a reason for hiding this comment

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

With the real tests added, this placeholder could be removed to reduce the noise.

@dashaaaa21
Copy link
Author

Thank you very much!

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