Skip to content

Assignments week 2 Ahmad Zetouny #10

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 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assignments/hackyourtemperature/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sources/keys.js

Choose a reason for hiding this comment

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

Great job on adding your api key in the gitignore!

44 changes: 44 additions & 0 deletions assignments/hackyourtemperature/__tests__/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import app from "../sources/app.js";
import * as keys from "../sources/keys.js";
import supertest from "supertest";
import fetch from "node-fetch";

const request = supertest(app);

Choose a reason for hiding this comment

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

Can you also add a test case for the GET / route?

describe("POST /weather", () => {
it("Checking for request body", (done) => {
request
.post("/weather")
.expect(400, { weatherText: "Request body is missing" }, done);
});

it("CityName should exist", (done) => {
request
.post("/weather")
.send({ City: "" })
.expect(400, { weatherText: "City name is missing" }, done);
});

it("CityName should be a string", (done) => {
request
.post("/weather")
.send({ cityName: 5 })
.expect(400, { weatherText: "City name should be a string" }, done);
});

it("Checking if the WeatherAPI is online", async () => {

Choose a reason for hiding this comment

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

There is no need for this test. This is a test that the developers of the weather API should write! You should be only testing the code you wrote to make sure it works as expected. I would replace the fetch call with a call to your api and then see if a 200 is returned!

const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=Arnhem,nl&appid=${keys.API_KEY}&units=metric`
);
expect(response.status).toBe(200);
});

it("Checking if temperature is a number", async () => {
const response = await fetch(

Choose a reason for hiding this comment

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

Same as above :) Call your endpoint!

`https://api.openweathermap.org/data/2.5/weather?q=Arnhem,nl&appid=${keys.API_KEY}&units=metric`
);
const json = await response.json();
const temp = json.main.temp;
expect(typeof temp).toBe("number");
});
});
13 changes: 13 additions & 0 deletions assignments/hackyourtemperature/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
presets: [
[
// This is a configuration, here we are telling babel what configuration to use
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
],
};
8 changes: 8 additions & 0 deletions assignments/hackyourtemperature/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
// Tells jest that any file that has 2 .'s in it and ends with either js or jsx should be run through the babel-jest transformer
transform: {
"^.+\\.jsx?$": "babel-jest",
},
// By default our `node_modules` folder is ignored by jest, this tells jest to transform those as well
transformIgnorePatterns: [],
};
25 changes: 25 additions & 0 deletions assignments/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "jest",
"start": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"express": "^5.1.0",
"express-handlebars": "^8.0.2",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"@babel/preset-env": "^7.27.1",
"babel-jest": "^29.7.0",
"jest": "^29.7.0",
"supertest": "^7.1.0"
}
}
7 changes: 7 additions & 0 deletions assignments/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import app from "./sources/app.js";

const PORT = 3000;

app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}`);
});
47 changes: 47 additions & 0 deletions assignments/hackyourtemperature/sources/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import express from "express";

Choose a reason for hiding this comment

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

Nice job on separating your app into server and app files, the app.js file should be in the main directory and not in sources to keep consistency!

import fetch from "node-fetch";
import * as keys from "./keys.js";

const app = express();

app.use(express.json());

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

app.post("/weather", async (req, res) => {
try {
if (!req.body) {
throw { weatherText: "Request body is missing" };
}

const { cityName } = req.body;

if (!cityName) {
throw { weatherText: "City name is missing" };
}

if (typeof cityName !== "string") {
throw { weatherText: "City name should be a string" };
}

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

if (!response.ok) {

Choose a reason for hiding this comment

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

Good work on checking the response code here! A suggestion would be to check if (!temp?.main?.temp) so that you are sure that the temperature is actually returned!

throw { weatherText: "City is not found!" };
}

const temp = await response.json();

res.status(200).json({
weatherText: `The temperature in ${cityName} is ${temp.main.temp}℃`,
});
} catch (error) {
res.status(400).json(error);
}
});

export default app;