diff --git a/assignments/config-files/hackyourtemperature/.gitignore b/assignments/config-files/hackyourtemperature/.gitignore new file mode 100644 index 000000000..9d968642b --- /dev/null +++ b/assignments/config-files/hackyourtemperature/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +.env +.DS_Store \ No newline at end of file diff --git a/assignments/config-files/hackyourtemperature/app.js b/assignments/config-files/hackyourtemperature/app.js new file mode 100644 index 000000000..853b65424 --- /dev/null +++ b/assignments/config-files/hackyourtemperature/app.js @@ -0,0 +1,42 @@ +import express from "express"; +import fetch from "node-fetch"; +import { API_KEY } from "./sources/keys.js"; +const app = express(); +app.use(express.json()); + +app.get("/", (req, res) => { + res.send("Hello from the backend "); +}); +app.post("/weather", async (req, res) => { + const { cityName } = req.body; + if (!cityName) { + return res.status(400).json({ Error: "Please provide a city name" }); + } + + try { + const response = await fetch( + `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}&units=metric` + ); + + const weatherData = await response.json(); + + if (response.status === 404) { + return res.status(404).json({ Error: "Invalid city name" }); + } else { + const weatherobj = { + Temp: Math.floor(weatherData.main.temp), + Feels: Math.floor(weatherData.main.feels_like), + Description: weatherData.weather[0].description, + }; + return res.json({ + Temperature: ` the temperature in ${cityName} is ${weatherobj.Temp}`, + Feels: `Feels like ${weatherobj.Feels}`, + Description: `${weatherobj.Description}`, + }); + } + } catch (error) { + res.status(500).json({ Error: "Server error, please try later" }); + } +}); + +export default app; diff --git a/assignments/config-files/hackyourtemperature/babel.config.cjs b/assignments/config-files/hackyourtemperature/babel.config.cjs new file mode 100644 index 000000000..fbb629af6 --- /dev/null +++ b/assignments/config-files/hackyourtemperature/babel.config.cjs @@ -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", + }, + }, + ], + ], +}; diff --git a/assignments/config-files/hackyourtemperature/jest.config.js b/assignments/config-files/hackyourtemperature/jest.config.js new file mode 100644 index 000000000..19ba9649e --- /dev/null +++ b/assignments/config-files/hackyourtemperature/jest.config.js @@ -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: [], +}; diff --git a/assignments/config-files/hackyourtemperature/package.json b/assignments/config-files/hackyourtemperature/package.json new file mode 100644 index 000000000..6f04ed667 --- /dev/null +++ b/assignments/config-files/hackyourtemperature/package.json @@ -0,0 +1,27 @@ +{ + "name": "hackyourtemperature", + "version": "1.0.0", + "main": "server.js", + "type": "module", + "scripts": { + "test": "jest", + "start": "node server.js", + "dev": "nodemon server.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "dotenv": "^16.4.7", + "express": "^4.21.2", + "node-fetch": "^3.3.2" + }, + "devDependencies": { + "@babel/preset-env": "^7.26.9", + "babel-jest": "^29.7.0", + "jest": "^29.7.0", + "nodemon": "^3.1.9", + "supertest": "^7.0.0" + } +} diff --git a/assignments/config-files/hackyourtemperature/postman_result.png b/assignments/config-files/hackyourtemperature/postman_result.png new file mode 100644 index 000000000..78f3fa949 Binary files /dev/null and b/assignments/config-files/hackyourtemperature/postman_result.png differ diff --git a/assignments/config-files/hackyourtemperature/server.js b/assignments/config-files/hackyourtemperature/server.js new file mode 100644 index 000000000..31a69a4c0 --- /dev/null +++ b/assignments/config-files/hackyourtemperature/server.js @@ -0,0 +1,4 @@ +import app from "./app.js"; + +const PORT = 3000; +app.listen(PORT, () => console.log(`Server is running on port ${PORT}!`)); diff --git a/assignments/config-files/hackyourtemperature/sources/keys.js b/assignments/config-files/hackyourtemperature/sources/keys.js new file mode 100644 index 000000000..b67b70d66 --- /dev/null +++ b/assignments/config-files/hackyourtemperature/sources/keys.js @@ -0,0 +1,3 @@ +import dotenv from "dotenv"; +dotenv.config(); +export const API_KEY = process.env.API_KEY; diff --git a/assignments/config-files/hackyourtemperature/tests/app.js b/assignments/config-files/hackyourtemperature/tests/app.js new file mode 100644 index 000000000..e69de29bb diff --git a/assignments/config-files/hackyourtemperature/tests/app.test.js b/assignments/config-files/hackyourtemperature/tests/app.test.js new file mode 100644 index 000000000..5f83cc1db --- /dev/null +++ b/assignments/config-files/hackyourtemperature/tests/app.test.js @@ -0,0 +1,27 @@ +import app from "../app.js"; +import supertest from "supertest"; + +const request = supertest(app); + +describe("POST /weather", () => { + it("should return weather data for a valid city name", async () => { + const response = await request.post("/weather").send({ + cityName: "Amsterdam", + }); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty("Temperature"); + }); + + it("should return an error if city name is not provided ", async () => { + const response = await request.post("/weather").send({}); + expect(response.status).toBe(400); + expect(response.body.Error).toBe("Please provide a city name"); + }); + it("should return 404 status error for invalid city names", async () => { + const response = await request.post("/weather").send({ + cityName: "NoCity", + }); + expect(response.status).toBe(404); + expect(response.body.Error).toBe("Invalid city name"); + }); +}); diff --git a/assignments/config-files/hackyourtemperature/week2_result.png b/assignments/config-files/hackyourtemperature/week2_result.png new file mode 100644 index 000000000..4851aff9b Binary files /dev/null and b/assignments/config-files/hackyourtemperature/week2_result.png differ diff --git a/package.json b/package.json new file mode 100644 index 000000000..da7e6cd93 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "dependencies": { + "express": "^4.21.2" + }, + "name": "node.js-cohort51", + "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", + "type": "module", + + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "nodemon": "^3.1.9" + } +}