forked from HackYourFuture/Node.js
-
Notifications
You must be signed in to change notification settings - Fork 10
Ahmad-w2-nodejs #11
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
AhmadSaadeddin1987
wants to merge
4
commits into
HackYourAssignment:main
Choose a base branch
from
AhmadSaadeddin1987:Ahmad-w2-Nodejs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ahmad-w2-nodejs #11
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| node_modules/ | ||
| .env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import express from "express"; | ||
| import keys from "./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; | ||
|
|
||
| if (!cityName) { | ||
| return res.status(400).json({ | ||
| weatherText: "City name is required!", | ||
| }); | ||
| } | ||
|
|
||
| try { | ||
| const response = await fetch( | ||
| `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${keys.API_KEY}&units=metric`, | ||
| ); | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| if (data.cod === "404") { | ||
| return res.status(404).json({ | ||
| weatherText: "City is not found!", | ||
| }); | ||
| } | ||
|
|
||
| res.json({ | ||
| weatherText: `The temperature in ${data.name} is ${data.main.temp}°C.`, | ||
| }); | ||
| } catch (error) { | ||
| res.status(500).json({ | ||
| weatherText: "Error fetching weather data.", | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| export default app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| }, | ||
| }, | ||
| ], | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| 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: [], | ||
| testEnvironment: "node", | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "name": "hackyourtemperature", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "server.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "jest", | ||
| "start": "node server.js", | ||
| "prettier:check": "prettier --check .", | ||
| "prettier:write": "prettier --write ." | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "dotenv": "^17.2.3", | ||
| "express": "^5.1.0", | ||
| "express-handlebars": "^8.0.3" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/preset-env": "^7.28.5", | ||
| "babel-jest": "^30.2.0", | ||
| "husky": "^9.1.7", | ||
| "jest": "^30.2.0", | ||
| "lint-staged": "^16.2.6", | ||
| "supertest": "^7.1.4" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import app from "./app.js"; | ||
|
|
||
| const PORT = process.env.PORT || 3000; | ||
|
|
||
| app.listen(PORT, () => { | ||
| console.log(`Server running on http://localhost:${PORT}`); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import dotenv from 'dotenv'; | ||
| dotenv.config(); | ||
|
|
||
| const keys = { | ||
| API_KEY: process.env.API_KEY | ||
| }; | ||
|
|
||
| export default keys; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import app from "../app.js"; | ||
| import supertest from "supertest"; | ||
|
|
||
| const request = supertest(app); | ||
|
|
||
| describe("POST /weather", () => { | ||
| it("should return 400 if no cityName is provided", async () => { | ||
| const response = await request.post("/weather").send({}); | ||
| expect(response.status).toBe(400); | ||
| expect(response.body.weatherText).toBe("City name is required!"); | ||
| }); | ||
|
|
||
| it("should return 404 if city is not found", async () => { | ||
| const response = await request | ||
| .post("/weather") | ||
| .send({ cityName: "UnknownCity" }); | ||
| expect(response.status).toBe(404); | ||
| expect(response.body.weatherText).toBe("City is not found!"); | ||
| }); | ||
|
|
||
| it("should return weather data for a valid city", async () => { | ||
| const response = await request | ||
| .post("/weather") | ||
| .send({ cityName: "Amsterdam" }); | ||
| expect(response.status).toBe(200); | ||
| expect(response.body.weatherText).toContain("Amsterdam"); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
NEVER EVER EVER UPLOAD YOUR API KEYS!!!!!!!!!! Instead put them either in an
.envfile, or use them as runtime arguments (API_KEY=123245 node server.jsand thenprocess.env.API_KEYin code)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.
I did it and add the last modification commit, it is very useful feedback because i made a big error with sharing my API_key