From 36162223883a2102d774fd1c472b15c5a388532a Mon Sep 17 00:00:00 2001 From: rizan-ibrahim Date: Tue, 18 Feb 2025 15:34:15 +0100 Subject: [PATCH 1/4] rizan-ibrahim-node.js-w1 --- assignments/HackYourTemperature/package.json | 18 ++++++++++++++++++ assignments/HackYourTemperature/server.js | 14 ++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 assignments/HackYourTemperature/package.json create mode 100644 assignments/HackYourTemperature/server.js diff --git a/assignments/HackYourTemperature/package.json b/assignments/HackYourTemperature/package.json new file mode 100644 index 000000000..8bf1bf4e2 --- /dev/null +++ b/assignments/HackYourTemperature/package.json @@ -0,0 +1,18 @@ +{ + "name": "hackyourtemperature", + "version": "1.0.0", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "express": "^4.21.2", + "express-handlebars": "^8.0.1", + "node-fetch": "^3.3.2" + } +} diff --git a/assignments/HackYourTemperature/server.js b/assignments/HackYourTemperature/server.js new file mode 100644 index 000000000..8ebafd2d9 --- /dev/null +++ b/assignments/HackYourTemperature/server.js @@ -0,0 +1,14 @@ +import express from "express"; +import { engine } from "express-handlebars"; +import fetch from "node-fetch"; + +const app = express(); +const PORT = 3000; + +app.get("/", (req, res) => { + res.send("hello from backend to frontend!"); +}); + +app.listen(PORT, () => { + console.log(`Server is running on http://localhost:${PORT}`); +}); From 7661fb07246d5fce74c6d4023c010e8373fce521 Mon Sep 17 00:00:00 2001 From: rizan-ibrahim Date: Tue, 18 Feb 2025 17:13:19 +0100 Subject: [PATCH 2/4] rizan-ibrahim-node.js-w1 --- assignments/HackYourTemperature/server.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/assignments/HackYourTemperature/server.js b/assignments/HackYourTemperature/server.js index 8ebafd2d9..5d9aeb3fc 100644 --- a/assignments/HackYourTemperature/server.js +++ b/assignments/HackYourTemperature/server.js @@ -1,14 +1,24 @@ import express from "express"; -import { engine } from "express-handlebars"; -import fetch from "node-fetch"; const app = express(); const PORT = 3000; +app.use(express.json()); + app.get("/", (req, res) => { res.send("hello from backend to frontend!"); }); +app.post("/weather", (req, res) => { + const cityName = req.body.cityName; + + if (!cityName) { + return res.status(400).json({ message: "City name is required." }); + } + + res.status(200).json({ cityName: cityName }); +}); + app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); }); From 69f9351dd75da9fe0e0b404b0a023436dca6f986 Mon Sep 17 00:00:00 2001 From: rizan-ibrahim Date: Tue, 25 Feb 2025 20:38:08 +0100 Subject: [PATCH 3/4] rizan-node.js-w2 --- .../HackYourTemperature/__tests__/app.test.js | 28 ++++++++++++ assignments/HackYourTemperature/app.js | 44 +++++++++++++++++++ .../HackYourTemperature/babel.config.cjs | 4 ++ .../HackYourTemperature/jest.config.js | 3 ++ assignments/HackYourTemperature/package.json | 12 +++-- assignments/HackYourTemperature/server.js | 19 +------- .../HackYourTemperature/sources/keys.js | 4 ++ cd | 0 8 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 assignments/HackYourTemperature/__tests__/app.test.js create mode 100644 assignments/HackYourTemperature/app.js create mode 100644 assignments/HackYourTemperature/babel.config.cjs create mode 100644 assignments/HackYourTemperature/jest.config.js create mode 100644 assignments/HackYourTemperature/sources/keys.js create mode 100644 cd diff --git a/assignments/HackYourTemperature/__tests__/app.test.js b/assignments/HackYourTemperature/__tests__/app.test.js new file mode 100644 index 000000000..b51a1e545 --- /dev/null +++ b/assignments/HackYourTemperature/__tests__/app.test.js @@ -0,0 +1,28 @@ +import request from "supertest"; +import app from "../server.js"; + +describe("POST /weather", () => { + it("should return a 400 error if cityName is missing", async () => { + const response = await request.post("/weather").send({}); + expect(response.status).toBe(400); + expect(response.body).toEqual({ message: "City name is required." }); + }); + + it("should return a 404 error if cityName is not found", async () => { + const response = await request + .post("/weather") + .send({ cityName: "invalidCity" }); + expect(response.status).toBe(404); + expect(response.body.weatherText).toBe("city is not found!"); + }); + + it("should return a valid weather response for a real city", async () => { + const response = await request + .post("/weather") + .send({ cityName: "Amsterdam" }); + expect(response.status).toBe(200); + expect(response.body.weatherText).toContain( + "The temperature in Amsterdam is" + ); + }); +}); diff --git a/assignments/HackYourTemperature/app.js b/assignments/HackYourTemperature/app.js new file mode 100644 index 000000000..cc8e6f9e1 --- /dev/null +++ b/assignments/HackYourTemperature/app.js @@ -0,0 +1,44 @@ +import express from "express"; +import keys from "./sources/keys.js"; +import fetch from "node-fetch"; + +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).json({ message: "City name is required." }); + } + const API_KEY = keys.API_KEY; + const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}&units=metric`; + + try { + const response = await fetch(url); + if (!response.ok) { + throw new Error(`API request failed with status ${response.status}`); + } + const data = await response.json(); + if (data.cod === "404") { + return res.status(404).json({ weatherText: "city is not found!" }); + } + + const temperature = data.main.temp; + res.status(200).json({ + weatherText: `The temperature in ${cityName} is ${temperature}°C`, + }); + } catch (error) { + console.error("Error fetching weather data:", error.message); + res + .status(500) + .json({ message: "An error occurred while fetching weather data." }); + } +}); + +export default app; diff --git a/assignments/HackYourTemperature/babel.config.cjs b/assignments/HackYourTemperature/babel.config.cjs new file mode 100644 index 000000000..edcf10310 --- /dev/null +++ b/assignments/HackYourTemperature/babel.config.cjs @@ -0,0 +1,4 @@ +module.exports = { + presets: ["@babel/preset-env", "@babel/preset-react"], + plugins: ["@babel/plugin-transform-modules-commonjs"], +}; diff --git a/assignments/HackYourTemperature/jest.config.js b/assignments/HackYourTemperature/jest.config.js new file mode 100644 index 000000000..013cf3358 --- /dev/null +++ b/assignments/HackYourTemperature/jest.config.js @@ -0,0 +1,3 @@ +export default { + transformIgnorePatterns: ["/node_modules/(?!node-fetch)"], +}; diff --git a/assignments/HackYourTemperature/package.json b/assignments/HackYourTemperature/package.json index 8bf1bf4e2..4e2ad21a4 100644 --- a/assignments/HackYourTemperature/package.json +++ b/assignments/HackYourTemperature/package.json @@ -1,10 +1,9 @@ { "name": "hackyourtemperature", "version": "1.0.0", - "main": "index.js", - "type": "module", + "main": "server.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "jest" }, "keywords": [], "author": "", @@ -14,5 +13,12 @@ "express": "^4.21.2", "express-handlebars": "^8.0.1", "node-fetch": "^3.3.2" + }, + "type": "module", + "devDependencies": { + "@babel/preset-env": "^7.26.9", + "babel-jest": "^29.7.0", + "jest": "^29.7.0", + "supertest": "^7.0.0" } } diff --git a/assignments/HackYourTemperature/server.js b/assignments/HackYourTemperature/server.js index 5d9aeb3fc..9df674eda 100644 --- a/assignments/HackYourTemperature/server.js +++ b/assignments/HackYourTemperature/server.js @@ -1,24 +1,7 @@ -import express from "express"; +import app from "./app.js"; -const app = express(); const PORT = 3000; -app.use(express.json()); - -app.get("/", (req, res) => { - res.send("hello from backend to frontend!"); -}); - -app.post("/weather", (req, res) => { - const cityName = req.body.cityName; - - if (!cityName) { - return res.status(400).json({ message: "City name is required." }); - } - - res.status(200).json({ cityName: cityName }); -}); - app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); }); diff --git a/assignments/HackYourTemperature/sources/keys.js b/assignments/HackYourTemperature/sources/keys.js new file mode 100644 index 000000000..72f84a5f3 --- /dev/null +++ b/assignments/HackYourTemperature/sources/keys.js @@ -0,0 +1,4 @@ +const keys = { + API_KEY: "6c6488cdc94811312595951b156b69ba", +}; +export default keys; diff --git a/cd b/cd new file mode 100644 index 000000000..e69de29bb From ff3fc4bad9bdcdd4aeeaa59e5678d10daf2ac611 Mon Sep 17 00:00:00 2001 From: rizan-ibrahim Date: Tue, 11 Mar 2025 14:49:00 +0100 Subject: [PATCH 4/4] rizan-node.js-w2 modifed --- assignments/HackYourTemperature/__tests__/app.test.js | 2 +- assignments/HackYourTemperature/app.js | 3 +++ assignments/HackYourTemperature/package.json | 1 + assignments/HackYourTemperature/sources/.env | 1 + assignments/HackYourTemperature/sources/keys.js | 7 ++++--- 5 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 assignments/HackYourTemperature/sources/.env diff --git a/assignments/HackYourTemperature/__tests__/app.test.js b/assignments/HackYourTemperature/__tests__/app.test.js index b51a1e545..4d3390c76 100644 --- a/assignments/HackYourTemperature/__tests__/app.test.js +++ b/assignments/HackYourTemperature/__tests__/app.test.js @@ -17,7 +17,7 @@ describe("POST /weather", () => { }); it("should return a valid weather response for a real city", async () => { - const response = await request + const response = await request(app) .post("/weather") .send({ cityName: "Amsterdam" }); expect(response.status).toBe(200); diff --git a/assignments/HackYourTemperature/app.js b/assignments/HackYourTemperature/app.js index cc8e6f9e1..b92414de2 100644 --- a/assignments/HackYourTemperature/app.js +++ b/assignments/HackYourTemperature/app.js @@ -17,6 +17,9 @@ app.post("/weather", async (req, res) => { return res.status(400).json({ message: "City name is required." }); } const API_KEY = keys.API_KEY; + if (!API_KEY) { + return res.status(500).json({ message: "API key is missing." }); + } const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}&units=metric`; try { diff --git a/assignments/HackYourTemperature/package.json b/assignments/HackYourTemperature/package.json index 4e2ad21a4..1efa7314a 100644 --- a/assignments/HackYourTemperature/package.json +++ b/assignments/HackYourTemperature/package.json @@ -10,6 +10,7 @@ "license": "ISC", "description": "", "dependencies": { + "dotenv": "^16.4.7", "express": "^4.21.2", "express-handlebars": "^8.0.1", "node-fetch": "^3.3.2" diff --git a/assignments/HackYourTemperature/sources/.env b/assignments/HackYourTemperature/sources/.env new file mode 100644 index 000000000..d728cb490 --- /dev/null +++ b/assignments/HackYourTemperature/sources/.env @@ -0,0 +1 @@ +API_KEY: "6c6488cdc94811312595951b156b69ba", \ No newline at end of file diff --git a/assignments/HackYourTemperature/sources/keys.js b/assignments/HackYourTemperature/sources/keys.js index 72f84a5f3..502f27440 100644 --- a/assignments/HackYourTemperature/sources/keys.js +++ b/assignments/HackYourTemperature/sources/keys.js @@ -1,4 +1,5 @@ -const keys = { - API_KEY: "6c6488cdc94811312595951b156b69ba", +import dotenv from "dotenv"; +dotenv.config(); +export const keys = { + API_KEY: process.env.API_KEY, }; -export default keys;