-
Notifications
You must be signed in to change notification settings - Fork 10
Majd hussein w2 node #12
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import supertest from "supertest"; | ||
| import app from "../app.js"; | ||
|
|
||
| const request = supertest(app); | ||
|
|
||
| describe("POST /weather", () => { | ||
| it("case1: (Valid city name) >>> should return weather data", async () => { | ||
| const response = await request | ||
| .post("/weather") | ||
| .send({ cityName: "London" }); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| }); | ||
|
|
||
| it("Case2: (Inavalid city name ) >>> should return 500 error", async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd argue that an invalid city is in fact a client error, not a server error. The response code I'd expect in this scenario is 404. |
||
| const response = await request | ||
| .post("/weather") | ||
| .send({ cityName: "Incorrect_CityName" }); | ||
|
|
||
| expect(response.status).toBe(500); | ||
| expect(response.body).toHaveProperty("error", "City is not found"); | ||
|
||
| }); | ||
|
|
||
| it("Case3: (Empty City Name) should return 500 error", async () => { | ||
|
||
| const response = await request.post("/weather").send({}); | ||
| expect(response.status).toBe(500); | ||
| expect(response.body).toHaveProperty("error", "City is not found"); | ||
|
||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import express from "express"; | ||
| import keys from "./sources/keys.js"; // to access it use keys.API_KEY | ||
| import fetch from "node-fetch"; | ||
| const app = express(); | ||
|
|
||
| export default app; | ||
|
||
|
|
||
| app.use(express.json()); | ||
|
|
||
| //GET request that sends the message [hello from backend to frontend!] to the client | ||
| app.get("/", (req, res) => { | ||
| res.send("Hello From Backend to Frontend"); | ||
| }); | ||
|
|
||
| // --------------5.1 Adding a POST request---------------- | ||
| app.post("/weather", (req, res) => { | ||
| const { cityName } = req.body; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add input validation, making sure a cityName is provided in the request. |
||
| const cityWeatherUrl = `http://api.openweathermap.org/data/2.5/forecast?q=${cityName}&appid=${keys.API_KEY}`; | ||
|
||
|
|
||
| fetchWeather(cityWeatherUrl); | ||
|
|
||
| async function fetchWeather(URL) { | ||
| try { | ||
| const response = await fetch(URL); | ||
| const data = await response.json(); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A good practice when working with APIs is to check for |
||
| if (data.cod === "404") { | ||
| throw new Error("City not found"); | ||
|
||
| } | ||
|
|
||
| const tempratureInCelsius = data.list[0].main.temp - 273.15; // Temperature in Celsius | ||
|
||
|
|
||
| res.json({ | ||
| weatherText: `Temperature in ${cityName}: ${tempratureInCelsius.toFixed( | ||
| 2 | ||
| )} °C`, | ||
| }); | ||
| } catch (error) { | ||
| res.status(500).json({ error: "City is not found" }); | ||
|
||
| } | ||
| } | ||
| }); | ||
|
|
||
| // | ||
| 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", | ||
| }, | ||
| }, | ||
| ], | ||
| ], | ||
| }; |
| 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: [], | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "name": "hackyourtemperature", | ||
| "type": "module", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "server.js", | ||
| "scripts": { | ||
| "test": "jest", | ||
| "start": "node server.js", | ||
| "dev": "nodemon server.js" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "devDependencies": { | ||
| "@babel/preset-env": "^7.28.5", | ||
| "babel-jest": "^30.2.0", | ||
| "express": "^5.1.0", | ||
|
||
| "jest": "^30.2.0", | ||
| "nodemon": "^3.1.10", | ||
| "supertest": "^7.1.4" | ||
| }, | ||
| "dependencies": { | ||
| "dotenv": "^17.2.3", | ||
| "express-handlebars": "^8.0.3", | ||
| "node-fetch": "^3.3.2" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import app from "./app.js"; | ||
|
|
||
| app.listen(3000, () => { | ||
| console.log("Server on Port 3000 is running"); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export default { API_KEY: "d63cf1893cfbd168dfb340b341a8c03e" }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding API keys in source code is acceptable for learning purposes, but in production, you should use environment variables (process.env.API_KEY) and add this file to .gitignore. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should also check response.body.weatherText contains the requested city name and temperature.