Skip to content

Assignments week 2 Ilia Bubnov #8

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

Conversation

MBreathe
Copy link

@MBreathe MBreathe commented May 4, 2025

No description provided.

Copy link

@EdwardSmart98 EdwardSmart98 left a comment

Choose a reason for hiding this comment

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

A very strong assignment you should be proud of, some very nice features are showing through like separation of logic, and testing of edge cases.
My biggest place for improvement is how can you better handle errors so that the front-end can use the messages in a valuable/consistent way.

res.send("hello from backend to frontend!");
});
app.post("/weather", (req, res) => {
getWeather(req, res);

Choose a reason for hiding this comment

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

This is really good seperation of logic, placing functions/controllers in a different class/method. Good Job!

const message = { weatherText: "" };
if (!req.body.cityName) {
message.weatherText = "You have to fill in city name";
return res.status(400).json(message);

Choose a reason for hiding this comment

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

We should use a different model for errors than we do for valid responses, as the client may not check the status code, and just if their is a weatherText value. A good practise is all errors get returned in an error object like :
{
"errorCode" : "INVALID_REQUEST",
"errorMessage" : "You have to fill in a city name"
}

This allows you to build standard error wrappers/handlers in both the back-end and front-end.

message.weatherText =
"Something went wrong on server side, try again later";
return res.status(e.status).json(message);
}

Choose a reason for hiding this comment

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

If the errors status is not set (e.g. the error was thrown from something other that the API request, then this API will not respond properly, good to write at the bottom a single "unknown error". response.

const request = supertest(app);

describe("POST /weather", () => {
it("Should return an error when cityName is not passed", async () => {

Choose a reason for hiding this comment

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

Good checking of edge cases.

@@ -0,0 +1,3 @@
export const keys = {

Choose a reason for hiding this comment

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

It's good practice to not push up API keys to repo's to keep them safe. two ways to do this are:
-> Ignoring this file in the .gitignore.
-> adding a .env file (and ignoring it) - This is better, as the code will still compile, and the .env file is a standard across a lot of software/programs, so loading them into the program at runtime is handleded well.

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