Skip to content

Samira w2 node.js #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

samira313
Copy link

Hello Samir,

In this pull request, I have made the following improvements:
• Refactored the project to use ES Modules by adding "type": "module" in package.json.
• Implemented a weather API using Express and node-fetch.
• Added error handling for invalid or missing city names.
• Created automated tests with Jest and Supertest to validate API functionality.

Looking forward to your feedback.

@eidosam eidosam self-assigned this Feb 26, 2025
@eidosam eidosam added To review Week 2 Week 2 assignment labels Feb 26, 2025
Copy link

@eidosam eidosam left a comment

Choose a reason for hiding this comment

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

You did very well in this week's assignment, Samira!

  • You used environment variables for storing the credentials instead of writing it in the code, that's a great security practice.
  • The test covers the main cases.
  • You used early-return pattern which improves code readability.

One thing is missing; the test configuration files, please add them, and fix the response structure, so we can mark this PR as approved.

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 gitignore file, but it should be named .gitignore without extension; see https://git-scm.com/docs/gitignore


// Post route to fetch weather data
app.post('/weather', async (req, res) => {
const { cityName } = req.body; // Extract city name from request body
Copy link

Choose a reason for hiding this comment

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

I would put the comment in its own line above the code.

    // Extract city name from request body
    const { cityName } = req.body;


if (!cityName) {
return res.status(400).json({ message: "City name is required" }); // Validate the request
}
Copy link

Choose a reason for hiding this comment

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

Same here, the comment in its own line. Also try to use chaining to improve code readability

    // Validate the request
    if (!cityName) {
        return res.status(400)
            .json({ message: "City name is required" });
    }

try {
// Fetch weather data from OpenWeatherMap API using provided city name and API key
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${keys.API_KEY}&units=metric`
Copy link

Choose a reason for hiding this comment

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

Since you have the BASE_URL, you use it here.

const data = await response.json();

if (data.cod !== 200) {
return res.status(404).json({ message: "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.

So great to see you use the early-return pattern; it makes the code much cleaner and readable.

res.json({
cityName : data.name,
temprature : 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.

In the instructions in week2/MAKEME.md, the return object should be

{
    weatherText: <message>
}

const { cityName } = req.body; // Extract city name from request body

if (!cityName) {
return res.status(400).json({ message: "City name is required" }); // Validate the request
Copy link

Choose a reason for hiding this comment

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

In the instructions in week2/MAKEME.md, the return object should be

{
    weatherText: "City is not found!"
}

const PORT = 3000;

// Start the server on the specified port
app.listen(PORT , () => {
Copy link

Choose a reason for hiding this comment

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

There is a redundant space after PORT.

@@ -0,0 +1,7 @@
import dotenv from "dotenv";
Copy link

Choose a reason for hiding this comment

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

Like a pro!

expect(response.body).toHaveProperty("message", "City name is required");
});
});

Copy link

Choose a reason for hiding this comment

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

That's great test coverage!

@samira313
Copy link
Author

samira313 commented Mar 4, 2025 via email

Copy link

@eidosam eidosam left a comment

Choose a reason for hiding this comment

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

Thank you, Samira, for addressing the feedback!
Could you please keep only one version in this PR? So you need to move the files from assignments/hackyourtemperature2 to assignments/hackyourtemperature and delete assignments/hackyourtemperature2.

"version": "1.0.0",
"main": "server.js",
"scripts": {
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
Copy link

Choose a reason for hiding this comment

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

If you move the test configuration files babel.config.cjs and jest.config.js next to the package.json, you won't need this dependency.

Copy link

@eidosam eidosam 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 addressing all the feedback, Samira! Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Approved Week 2 Week 2 assignment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants