Skip to content

Conversation

@Majoodeh
Copy link

@Majoodeh Majoodeh commented Nov 5, 2025

No description provided.

@durw4rd durw4rd self-assigned this Nov 11, 2025
@durw4rd
Copy link

durw4rd commented Nov 11, 2025

Hey @Majoodeh , it appears the committed files are incomplete. I see you've added the Weather API key and removed the code from last week, but no further changes to the /weather endpoint show up.

Tasks from here onwards are missing: https://github.com/HackYourAssignment/Node.js-Cohort54/blob/main/week2/MAKEME.md#312-fetch-it-from-our-api

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 adding the missing files, and for your work on this assignment, Majd. I've added a few comments, and I would like you to review & implement the suggestions.
While you have implemented the instructions, there are just too many inaccuracies for me to accept the work as is.

"devDependencies": {
"@babel/preset-env": "^7.28.5",
"babel-jest": "^30.2.0",
"express": "^5.1.0",
Copy link

Choose a reason for hiding this comment

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

Since Express is required at app runtime, it should be installed as a dependency, not just devDependency.

import fetch from "node-fetch";
const app = express();

export default app;
Copy link

Choose a reason for hiding this comment

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

Best practice is to list exports at the end of the file. Exporting before the app's routes are defined means that at the time of the export, the app doesn't include any routes, etc.
Since ES modules are evaluated asynchronously, your app still works before the app is imported in the server module, and all routes have been added, but it's better to follow the generally used pattern.


// --------------5.1 Adding a POST request----------------
app.post("/weather", (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.

It would be good to add input validation, making sure a cityName is provided in the request.

if (!cityName || typeof cityName !== "string" || !cityName.trim()) {
    return res.status(400).json({ weatherText: "Please provide a cityName." });
}

// --------------5.1 Adding a POST request----------------
app.post("/weather", (req, res) => {
const { cityName } = req.body;
const cityWeatherUrl = `http://api.openweathermap.org/data/2.5/forecast?q=${cityName}&appid=${keys.API_KEY}`;
Copy link

Choose a reason for hiding this comment

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

It doesn't make much difference from the perspective of learning how to work with APIs, but note that the instructions ask to use a different endpoint: https://api.openweathermap.org/data/2.5/**weather**.

try {
const response = await fetch(URL);
const data = await response.json();

Copy link

Choose a reason for hiding this comment

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

A good practice when working with APIs is to check for !response.ok with correct error handling.

.post("/weather")
.send({ cityName: "London" });

expect(response.status).toBe(200);
Copy link

Choose a reason for hiding this comment

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

Should also check response.body.weatherText contains the requested city name and temperature.

expect(response.status).toBe(200);
});

it("Case2: (Inavalid city name ) >>> should return 500 error", async () => {
Copy link

Choose a reason for hiding this comment

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

I'd argue that an invalid city is in fact a client error, not a server error. The response code I'd expect in this scenario is 404.

.send({ cityName: "Incorrect_CityName" });

expect(response.status).toBe(500);
expect(response.body).toHaveProperty("error", "City is not found");
Copy link

Choose a reason for hiding this comment

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

A more accurate statement is expect(response.body.weatherText).toBe("City is not found!");

it("Case3: (Empty City Name) should return 500 error", async () => {
const response = await request.post("/weather").send({});
expect(response.status).toBe(500);
expect(response.body).toHaveProperty("error", "City is not found");
Copy link

Choose a reason for hiding this comment

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

A more accurate statement is expect(response.body.weatherText).toContain("Please provide a cityName");

expect(response.body).toHaveProperty("error", "City is not found");
});

it("Case3: (Empty City Name) should return 500 error", async () => {
Copy link

Choose a reason for hiding this comment

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

Empty cityName should return 400 (Bad Request), not 500.
400 = client error (invalid input), 500 = server error (internal problem)

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 making the changes, Majd - it looks good to me now!

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