diff --git a/.gitignore b/.gitignore index b6b402ed9..21c218cae 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,6 @@ npm-debug.log package-lock.json yarn-error.log *.bkp +.env week3/prep-exercise/server-demo/ diff --git a/assignments/hackyourtemperature/__tests__/app.test.js b/assignments/hackyourtemperature/__tests__/app.test.js new file mode 100644 index 000000000..a00486210 --- /dev/null +++ b/assignments/hackyourtemperature/__tests__/app.test.js @@ -0,0 +1,47 @@ +import { app } from "../app.js"; +import supertest from "supertest"; + +const request = supertest(app); + +describe("GET /", () => { + + it("Send welcome page", async () => { + const response = await request.get("/"); + expect(response.status).toBe(200); + expect(response.text).toBe('Hello from backend to frontend'); + }); + +}); + +describe("POST /weather", () => { + + it("Valid city name provided", async () => { + const response = await request.post("/weather").send({ + city: "Maastricht", + }); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty("cityName"); + expect(response.body.cityName).toContain("Maastricht"); + }); + + + it("City could not be found", async () => { + const response = await request.post("/weather").send({ + city: "gfdhsdfdahh", + }); + expect(response.status).toBe(404); + expect(response.body).toHaveProperty("weatherText"); + expect(response.body).toEqual({ weatherText: 'City name not found!' }); + }); + + it("No city name provided", async () => { + const response = await request.post("/weather").send({ + city: "", + }); + expect(response.status).toBe(400); + expect(response.body).toHaveProperty("weatherText"); + expect(response.body).toEqual({ weatherText: "City name required" }); + }); + +}); + diff --git a/assignments/hackyourtemperature/app.js b/assignments/hackyourtemperature/app.js new file mode 100644 index 000000000..aeacab640 --- /dev/null +++ b/assignments/hackyourtemperature/app.js @@ -0,0 +1,33 @@ +import express from 'express'; +import { keys } from './sources/keys.js'; + +export const app = express(); + +app.use(express.json()); + +app.get('/', async (req, res) => { + return res.status(200).send('Hello from backend to frontend'); +}); + +app.post('/weather', async (req, res) => { + const { city } = req.body; + + if (!city) { + return res.status(400).json({ weatherText: "City name required" }); + } + + try { + const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${keys.API_KEY}`); + const data = await response.json(); + console.log(data) + + if (Number(data.cod) === 404) { + return res.status(404).json({ weatherText: 'City name not found!' }); + } + + return res.status(200).json({ cityName: `${data.name}`, temperature: `${data.main.temp}`}); + + } catch (err) { + return res.status(500).send('Error fetching'); + } +}); \ No newline at end of file diff --git a/assignments/hackyourtemperature/babel.config.cjs b/assignments/hackyourtemperature/babel.config.cjs new file mode 100644 index 000000000..fbb629af6 --- /dev/null +++ b/assignments/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/hackyourtemperature/jest.config.js b/assignments/hackyourtemperature/jest.config.js new file mode 100644 index 000000000..19ba9649e --- /dev/null +++ b/assignments/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/hackyourtemperature/package.json b/assignments/hackyourtemperature/package.json new file mode 100644 index 000000000..3dc0605dd --- /dev/null +++ b/assignments/hackyourtemperature/package.json @@ -0,0 +1,26 @@ +{ + "name": "hackyourtemperature", + "version": "1.0.0", + "main": "server.js", + "scripts": { + "test": "jest", + "start": "node server.js" + }, + "keywords": [], + "type": "module", + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "dotenv": "^16.4.7", + "express": "^4.21.2", + "express-handlebars": "^8.0.1", + "node-fetch": "^3.3.2" + }, + "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 new file mode 100644 index 000000000..e8e6e704f --- /dev/null +++ b/assignments/hackyourtemperature/server.js @@ -0,0 +1,7 @@ +import { app } from "./app.js"; + +const PORT = 3000; + +app.listen(PORT, () => { + console.log(`Server listening on port ${PORT}`); +}); \ No newline at end of file diff --git a/assignments/hackyourtemperature/sources/keys.js b/assignments/hackyourtemperature/sources/keys.js new file mode 100644 index 000000000..5c2f0a851 --- /dev/null +++ b/assignments/hackyourtemperature/sources/keys.js @@ -0,0 +1,5 @@ +import dotenv from 'dotenv'; + +dotenv.config(); + +export const keys = process.env; \ No newline at end of file