Skip to content

Glib-w2-NodeJS #22

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 2 commits into
base: main
Choose a base branch
from
Open

Conversation

gsaw01
Copy link

@gsaw01 gsaw01 commented Feb 25, 2025

No description provided.

@xed-euteon xed-euteon added To review Week 2 Week 2 assignment labels Mar 3, 2025
@xed-euteon xed-euteon self-assigned this Mar 3, 2025
Copy link

@xed-euteon xed-euteon left a comment

Choose a reason for hiding this comment

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

Great work,

I've left you some comments, please feel free to update the code, I'm gonna be requesting changes as I see it as a good idea to have a look.

not gonna take you long, just wanna be sure you have gotten good enough grasp of the concepts.

Comment on lines +1 to +3
export const keys = {
API_KEY: 'afc0f7157bda937505e237c68802afa5'
}

Choose a reason for hiding this comment

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

this is the easy way as requested by the homework description, have you thought of a bit safer more reliable way? maybe you wanna lookup dotenv?

Copy link

@saadkhaleeq610 saadkhaleeq610 Mar 8, 2025

Choose a reason for hiding this comment

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

exposing an API key in your source code is a security risk 🚨

Anyone can steal and misuse it.

The best practice is to hide the API key.

npm install dotenv

Move API key to a .env file (create this in your project root):

and write in it:

API_KEY=afc0f7157bda937505e237c68802afa5

then you can import the dotenv to securely import that api key. Something like this below.

import dotenv from 'dotenv';
dotenv.config();

export const keys = {
  API_KEY: process.env.API_KEY
};

Comment on lines +6 to +9
"scripts": {
"test": "jest",
"start": "nodemon server.js"
},

Choose a reason for hiding this comment

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

Love it.

Comment on lines +30 to +35
const response = await request
.post("/weather")
.send({ cityName });

expect(response.status).toBe(200);
expect(response.body.weatherText).toBe("City not found!");

Choose a reason for hiding this comment

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

in such a case don't you think that a 404 Not found as a good fit?

Choose a reason for hiding this comment

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

Your test expects a 200 status code when a city is not found, but logically, it should return 404 (Not Found) instead.

Fix: update it to return res.status(404).json({ error: "City not found!" })
instead of:
res.json({ weatherText: "City not found!" }).

Choose a reason for hiding this comment

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

Use .toMatchObject() Instead of .toBe() for JSON Comparisons.

Update it to: expect(response.body).toMatchObject({ error: "cityName is required" });

const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${keys.API_KEY}&units=metric`);
const data = await response.json();

if (data.cod !== 200) return res.json({ weatherText: 'City not found!' });

Choose a reason for hiding this comment

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

it's a good idea to check for the response.ok and it's not always a check for a 200, not everything comes as okay or not, thus you might wanna change the returned status code to be a good match to the response type. but for basic use and learning purposes only you might just wanna keep it meaningful to the message sent.

have you thought of 404?

Choose a reason for hiding this comment

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

  1. Check for 404, not 200 in the if condition.
  2. data.main.temp might be undefined if the API request fails or returns an unexpected response.
    Calling Math.floor(undefined) will result in NaN, which is not user-friendly.
  3. You're catching errors but not logging them, which makes debugging harder.

console.error(error); // Logs the error in the console
res.status(500).json({ error: 'Server-side error.' });

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

Successfully merging this pull request may close these issues.

3 participants