Skip to content
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
46 changes: 46 additions & 0 deletions assignments/config-files/hackyourtemperature/__tests__/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const request = require("supertest");

jest.mock("node-fetch", () => jest.fn());

const fetch = require("node-fetch");
const app = require("../app.js");

describe("POST /weather", () => {
it("should return weather text for a valid city", async () => {
fetch.mockResolvedValue({
json: async () => ({
main: { temp: 280 },
cod: 200,
}),
});

const response = await request(app)
.post("/weather")
.send({ cityName: "London" });

expect(response.statusCode).toBe(200);
expect(response.body.weatherText).toContain("The weather in London");
});

it("should return an error if city name is missing", async () => {
const response = await request(app).post("/weather").send({});

expect(response.statusCode).toBe(400);
expect(response.body.weatherText).toBe("City name is required!");
});

it("should return city not found for invalid city", async () => {
// نرجّع كود 404 من الـ API
fetch.mockResolvedValue({
json: async () => ({
cod: "404",
}),
});

const response = await request(app)
.post("/weather")
.send({ cityName: "asdkjashdkjashd" });

expect(response.body.weatherText).toBe("City is not found!");
});
});
40 changes: 40 additions & 0 deletions assignments/config-files/hackyourtemperature/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const express = require("express");
const fetch = require("node-fetch");
const keys = require("./sources/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) => {
const cityName = req.body.cityName;

if (!cityName) {
return res.status(400).send({ weatherText: "City name is required!" });
}

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

const data = await response.json();

if (data.cod === "404") {
return res.send({ weatherText: "City is not found!" });
}

const temperature = data.main.temp;

res.send({
weatherText: `The weather in ${cityName} is ${temperature}K`,
});
} catch (error) {
res.status(500).send({ weatherText: "Server error" });
}
});

module.exports = app;
18 changes: 18 additions & 0 deletions assignments/config-files/hackyourtemperature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "hackyourtemperature",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node server.js",
"test": "jest"
},
"dependencies": {
"express": "^5.1.0",
"express-handlebars": "^8.0.3",
"node-fetch": "^2.7.0"
},
"devDependencies": {
"jest": "^29.7.0",
"supertest": "^7.1.4"
}
}
7 changes: 7 additions & 0 deletions assignments/config-files/hackyourtemperature/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import app from "./app.js";

const PORT = 3000;

app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
3 changes: 3 additions & 0 deletions assignments/config-files/hackyourtemperature/sources/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const keys = {
API_KEY: "bd21a4fa2fde54d62c4e7df4d764ef31"
};
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "node.js-cohort54",
"version": "1.0.0",
"description": "> If you are following the HackYourFuture curriculum we recommend you to start with module 1: [HTML/CSS/GIT](https://github.com/HackYourFuture/HTML-CSS). To get a complete overview of the HackYourFuture curriculum first, click [here](https://github.com/HackYourFuture/curriculum).",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Hadidreem17/Node.js-Cohort54.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Hadidreem17/Node.js-Cohort54/issues"
},
"homepage": "https://github.com/Hadidreem17/Node.js-Cohort54#readme"
}