From ae15d8fea9208a82ae757ddd80c5de653ae0308d Mon Sep 17 00:00:00 2001 From: Yana Seniuk Date: Sun, 27 Apr 2025 02:16:48 +0200 Subject: [PATCH 01/13] feat: Complete tasks for prepEx --- .../1-pad-numbers/padLeft.js | 2 +- .../1-pad-numbers/script.js | 19 +++---- .../practice-exercises/2-left-pad/padLeft.js | 2 - week1/practice-exercises/2-left-pad/script.js | 14 ++--- week1/practice-exercises/package.json | 16 ++++++ week1/prep-exercises/1-web-server/index.html | 1 + .../prep-exercises/1-web-server/package.json | 12 +++-- week1/prep-exercises/1-web-server/server.js | 52 +++++++++++++++---- week1/prep-exercises/1-web-server/style.css | 3 ++ 9 files changed, 83 insertions(+), 38 deletions(-) create mode 100644 week1/practice-exercises/package.json create mode 100644 week1/prep-exercises/1-web-server/style.css diff --git a/week1/practice-exercises/1-pad-numbers/padLeft.js b/week1/practice-exercises/1-pad-numbers/padLeft.js index b58f887a3..2436068e1 100644 --- a/week1/practice-exercises/1-pad-numbers/padLeft.js +++ b/week1/practice-exercises/1-pad-numbers/padLeft.js @@ -4,7 +4,7 @@ * e.g. padLeft('foo', 5, '_') -> '__foo' * e.g. padLeft( '2', 2, '0') -> '02' */ -function padLeft(val, num, str) { +export function padLeft(val, num, str) { return '00000'.replace(/0/g, str).slice(0, num - val.length) + val; } diff --git a/week1/practice-exercises/1-pad-numbers/script.js b/week1/practice-exercises/1-pad-numbers/script.js index c11264e39..c0d907c46 100644 --- a/week1/practice-exercises/1-pad-numbers/script.js +++ b/week1/practice-exercises/1-pad-numbers/script.js @@ -1,21 +1,22 @@ - +import { padLeft } from "./padLeft.js"; /** - ** Exercise 1: Pad numbers - * + * + * + ** Exercise 1: Pad numbers + * * In this file use the padLeft function from padLeft.js to * pad the numbers to exactly 5 spaces and log them to the console - * + * * Expected output (replace the underscore with spaces): - * + * * ___12; * __846; * ____2; * _1236; - * + * * Tips: * where to use `exports` and where `require`? */ -let numbers = [ "12", "846", "2", "1236" ]; - -// YOUR CODE GOES HERE +let numbers = ["12", "846", "2", "1236"]; +numbers.forEach((num) => console.log(padLeft(num, 5, "_"))); diff --git a/week1/practice-exercises/2-left-pad/padLeft.js b/week1/practice-exercises/2-left-pad/padLeft.js index b58f887a3..0b7c83b8d 100644 --- a/week1/practice-exercises/2-left-pad/padLeft.js +++ b/week1/practice-exercises/2-left-pad/padLeft.js @@ -7,5 +7,3 @@ function padLeft(val, num, str) { return '00000'.replace(/0/g, str).slice(0, num - val.length) + val; } - -// YOUR CODE GOES HERE \ No newline at end of file diff --git a/week1/practice-exercises/2-left-pad/script.js b/week1/practice-exercises/2-left-pad/script.js index e0e081243..49f50a6b9 100644 --- a/week1/practice-exercises/2-left-pad/script.js +++ b/week1/practice-exercises/2-left-pad/script.js @@ -1,13 +1,5 @@ -/** - ** Exercise 2: To the left, to the left... - * - * Copy and paste your code from the previous exercise. - * Replace the function `padLeft` to use - * this new NPM package called `left-pad` instead then - * Pad the numbers to 8 characters to confirm that it works correctly - * - */ +import padLeft from "left-pad"; -let numbers = [ "12", "846", "2", "1236" ]; +let numbers = ["12", "846", "2", "1236"]; -// YOUR CODE GOES HERE \ No newline at end of file +numbers.forEach((num) => console.log(padLeft(num, 8, "_"))); diff --git a/week1/practice-exercises/package.json b/week1/practice-exercises/package.json new file mode 100644 index 000000000..b06fbd92c --- /dev/null +++ b/week1/practice-exercises/package.json @@ -0,0 +1,16 @@ +{ + "name": "practice-exercises", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "left-pad": "^1.3.0" + } +} diff --git a/week1/prep-exercises/1-web-server/index.html b/week1/prep-exercises/1-web-server/index.html index c64f7dcfa..0b326f19d 100644 --- a/week1/prep-exercises/1-web-server/index.html +++ b/week1/prep-exercises/1-web-server/index.html @@ -1,5 +1,6 @@ + My First Web Server diff --git a/week1/prep-exercises/1-web-server/package.json b/week1/prep-exercises/1-web-server/package.json index 30da55a2d..4ef406a38 100644 --- a/week1/prep-exercises/1-web-server/package.json +++ b/week1/prep-exercises/1-web-server/package.json @@ -3,12 +3,14 @@ "version": "1.0.0", "description": "A simple express server", "main": "server.js", + "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "nodemon server.js" }, - "author": "Andrej Gajduk", - "license": "MIT", - "dependencies": { - "express": "^4.17.1" + "author": "Yana Seniuk", + "license": "ISC", + "devDependencies": { + "nodemon": "^3.1.10" } } diff --git a/week1/prep-exercises/1-web-server/server.js b/week1/prep-exercises/1-web-server/server.js index 90cb5ee65..bcf5ff65a 100644 --- a/week1/prep-exercises/1-web-server/server.js +++ b/week1/prep-exercises/1-web-server/server.js @@ -1,14 +1,46 @@ -/** - * Exercise 3: Create an HTTP web server - */ +import * as http from "node:http"; +import * as path from "node:path"; +import { fileURLToPath } from "node:url"; +import * as fs from "node:fs/promises"; -const http = require('http'); +const PORT = 3000; -//create a server -let server = http.createServer(function (req, res) { - // YOUR CODE GOES IN HERE - res.write('Hello World!'); // Sends a response back to the client - res.end(); // Ends the response +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +async function handleRoute(file, contentType, status, req, res) { + try { + const filePath = path.join(__dirname, file); + const fileContent = await fs.readFile(filePath, "utf-8"); + + res.setHeader("Content-type", contentType); + res.statusCode = status; + res.end(fileContent); + } catch (err) { + res.setHeader("Content-type", "text/plain"); + res.statusCode = 500; + res.end("Server Error"); + } +} + +const server = http.createServer((req, res) => { + const routes = { + "/": { file: "index.html", contentType: "text/html" }, + "/index.js": { file: "index.js", contentType: "application/javascript" }, + "/style.css": { file: "style.css", contentType: "text/css" }, + }; + + const route = routes[req.url]; + if (route) { + handleRoute(route.file, route.contentType, 200, req, res); + } + + if (!route) { + res.statusCode = 404; + res.end("Not Found"); + } }); -server.listen(3000); // The server starts to listen on port 3000 +server.listen(PORT, () => { + console.log(`Server is running at http://127.0.0.7:${PORT}/`); +}); diff --git a/week1/prep-exercises/1-web-server/style.css b/week1/prep-exercises/1-web-server/style.css new file mode 100644 index 000000000..d599537da --- /dev/null +++ b/week1/prep-exercises/1-web-server/style.css @@ -0,0 +1,3 @@ +#content{ + color: green; +} \ No newline at end of file From 21cb19e9ec83753e6daa2f9da430fee031b8f5bc Mon Sep 17 00:00:00 2001 From: Yana <88456001+yanaesher@users.noreply.github.com> Date: Mon, 28 Apr 2025 09:23:13 +0200 Subject: [PATCH 02/13] Update server.js --- week1/prep-exercises/1-web-server/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week1/prep-exercises/1-web-server/server.js b/week1/prep-exercises/1-web-server/server.js index bcf5ff65a..31d078165 100644 --- a/week1/prep-exercises/1-web-server/server.js +++ b/week1/prep-exercises/1-web-server/server.js @@ -42,5 +42,5 @@ const server = http.createServer((req, res) => { }); server.listen(PORT, () => { - console.log(`Server is running at http://127.0.0.7:${PORT}/`); + console.log(`Server is running at http://127.0.0.1:${PORT}/`); }); From e223aa0551bc2c080d82225cd274de046e896a6c Mon Sep 17 00:00:00 2001 From: Yana Seniuk Date: Wed, 30 Apr 2025 16:33:54 +0200 Subject: [PATCH 03/13] feat: complete week 1 assignment --- assignments/hackyourtemperature/.gitignore | 4 ++++ assignments/hackyourtemperature/package.json | 18 ++++++++++++++++++ assignments/hackyourtemperature/src/server.js | 17 +++++++++++++++++ .../src/temperature.controller.js | 19 +++++++++++++++++++ .../src/temperature.service.js | 5 +++++ 5 files changed, 63 insertions(+) create mode 100644 assignments/hackyourtemperature/.gitignore create mode 100644 assignments/hackyourtemperature/package.json create mode 100644 assignments/hackyourtemperature/src/server.js create mode 100644 assignments/hackyourtemperature/src/temperature.controller.js create mode 100644 assignments/hackyourtemperature/src/temperature.service.js diff --git a/assignments/hackyourtemperature/.gitignore b/assignments/hackyourtemperature/.gitignore new file mode 100644 index 000000000..9a3d3c991 --- /dev/null +++ b/assignments/hackyourtemperature/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +.env +.vscode +npm-debug.log \ No newline at end of file diff --git a/assignments/hackyourtemperature/package.json b/assignments/hackyourtemperature/package.json new file mode 100644 index 000000000..d804045de --- /dev/null +++ b/assignments/hackyourtemperature/package.json @@ -0,0 +1,18 @@ +{ + "name": "hackyourtemperature", + "version": "1.0.0", + "description": "", + "main": "server.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node --watch --env-file=.env src/server.js" + }, + "author": "yanaesher", + "license": "ISC", + "devDependencies": { + "express": "^5.1.0", + "express-handlebars": "^8.0.3", + "node-fetch": "^3.3.2" + } +} diff --git a/assignments/hackyourtemperature/src/server.js b/assignments/hackyourtemperature/src/server.js new file mode 100644 index 000000000..74850cd2f --- /dev/null +++ b/assignments/hackyourtemperature/src/server.js @@ -0,0 +1,17 @@ +import express from "express"; +import { temperatureRouter } from "./temperature.controller.js"; + +const app = express(); +const PORT = process.env.PORT || 3000; + +function main() { + app.use(express.json()); + + app.use("/api/weather", temperatureRouter); + + app.listen(PORT, () => { + console.log(`Server is running at http://127.0.0.1:${PORT}`); + }); +} + +main(); diff --git a/assignments/hackyourtemperature/src/temperature.controller.js b/assignments/hackyourtemperature/src/temperature.controller.js new file mode 100644 index 000000000..983ee6295 --- /dev/null +++ b/assignments/hackyourtemperature/src/temperature.controller.js @@ -0,0 +1,19 @@ +import { Router } from "express"; +import { TemperatureService } from "./temperature.service.js"; +const temperatureRouter = Router(); +const temperatureService = new TemperatureService(); + +temperatureRouter.post("/", (req, res) => { + const { cityName } = req.body; + + if (!cityName) + return res.status(400).json({ + error: "Bad Request", + message: "Required parameter 'title' is missing", + }); + + const result = temperatureService.getCityName(cityName); + res.status(200).json(cityName); +}); + +export { temperatureRouter }; diff --git a/assignments/hackyourtemperature/src/temperature.service.js b/assignments/hackyourtemperature/src/temperature.service.js new file mode 100644 index 000000000..e5ac389b4 --- /dev/null +++ b/assignments/hackyourtemperature/src/temperature.service.js @@ -0,0 +1,5 @@ +export class TemperatureService { + getCityName(cityName) { + return cityName; + } +} From 793c1243e992551795f8bdb125b4193355690ccc Mon Sep 17 00:00:00 2001 From: Yana Seniuk Date: Wed, 30 Apr 2025 17:03:48 +0200 Subject: [PATCH 04/13] feat: add GET route and error handler --- assignments/hackyourtemperature/src/server.js | 6 +++++- .../hackyourtemperature/src/temperature.controller.js | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/assignments/hackyourtemperature/src/server.js b/assignments/hackyourtemperature/src/server.js index 74850cd2f..62426d8cd 100644 --- a/assignments/hackyourtemperature/src/server.js +++ b/assignments/hackyourtemperature/src/server.js @@ -7,7 +7,11 @@ const PORT = process.env.PORT || 3000; function main() { app.use(express.json()); - app.use("/api/weather", temperatureRouter); + app.use("/api", temperatureRouter); + + app.use((req, res) => { + res.status(404).json({ message: "Not found" }); + }); app.listen(PORT, () => { console.log(`Server is running at http://127.0.0.1:${PORT}`); diff --git a/assignments/hackyourtemperature/src/temperature.controller.js b/assignments/hackyourtemperature/src/temperature.controller.js index 983ee6295..0fb4deebf 100644 --- a/assignments/hackyourtemperature/src/temperature.controller.js +++ b/assignments/hackyourtemperature/src/temperature.controller.js @@ -3,6 +3,10 @@ import { TemperatureService } from "./temperature.service.js"; const temperatureRouter = Router(); const temperatureService = new TemperatureService(); +temperatureRouter.get("/", (req, res) => { + res.status(200).send("hello from backend to frontend!"); +}); + temperatureRouter.post("/", (req, res) => { const { cityName } = req.body; From d0bc139053ca73c2ac4016aca587e2b003e2684c Mon Sep 17 00:00:00 2001 From: Yana Seniuk Date: Wed, 30 Apr 2025 17:08:48 +0200 Subject: [PATCH 05/13] fix: update route/weather post path --- assignments/hackyourtemperature/package.json | 1 - assignments/hackyourtemperature/src/temperature.controller.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/assignments/hackyourtemperature/package.json b/assignments/hackyourtemperature/package.json index d804045de..99fc6585a 100644 --- a/assignments/hackyourtemperature/package.json +++ b/assignments/hackyourtemperature/package.json @@ -5,7 +5,6 @@ "main": "server.js", "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", "start": "node --watch --env-file=.env src/server.js" }, "author": "yanaesher", diff --git a/assignments/hackyourtemperature/src/temperature.controller.js b/assignments/hackyourtemperature/src/temperature.controller.js index 0fb4deebf..8b7363c2b 100644 --- a/assignments/hackyourtemperature/src/temperature.controller.js +++ b/assignments/hackyourtemperature/src/temperature.controller.js @@ -7,7 +7,7 @@ temperatureRouter.get("/", (req, res) => { res.status(200).send("hello from backend to frontend!"); }); -temperatureRouter.post("/", (req, res) => { +temperatureRouter.post("/weather", (req, res) => { const { cityName } = req.body; if (!cityName) From 872594c01f0a4ebb65ec793125aa1ea1139ad567 Mon Sep 17 00:00:00 2001 From: Yana Seniuk Date: Wed, 30 Apr 2025 17:15:20 +0200 Subject: [PATCH 06/13] fix:: return the result from the service in the POST /weather endpoint --- assignments/hackyourtemperature/src/temperature.controller.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assignments/hackyourtemperature/src/temperature.controller.js b/assignments/hackyourtemperature/src/temperature.controller.js index 8b7363c2b..4125dc86d 100644 --- a/assignments/hackyourtemperature/src/temperature.controller.js +++ b/assignments/hackyourtemperature/src/temperature.controller.js @@ -13,11 +13,11 @@ temperatureRouter.post("/weather", (req, res) => { if (!cityName) return res.status(400).json({ error: "Bad Request", - message: "Required parameter 'title' is missing", + message: "Required parameter 'cityName' is missing", }); const result = temperatureService.getCityName(cityName); - res.status(200).json(cityName); + res.status(200).json(result); }); export { temperatureRouter }; From abff4fcd9faa7b81b42021c9a0f2c2ea1e8d7bd9 Mon Sep 17 00:00:00 2001 From: Yana Seniuk Date: Fri, 2 May 2025 16:25:42 +0200 Subject: [PATCH 07/13] feat: week2 prep ex --- assignments/config-files/babel.config.cjs | 13 --- assignments/config-files/jest.config.js | 8 -- assignments/hackyourtemperature/.env | 1 + .../1-joke-api/package.json | 13 +++ week2/practice-exercises/1-joke-api/script.js | 26 +++--- week2/prep-exercises/1-blog-API/MamaFile | 1 + .../1-blog-API/blog.controller.js | 84 +++++++++++++++++++ week2/prep-exercises/1-blog-API/package.json | 4 +- week2/prep-exercises/1-blog-API/server.js | 26 ++++-- .../1-blog-API/services/blog.serice.js | 5 ++ 10 files changed, 133 insertions(+), 48 deletions(-) delete mode 100644 assignments/config-files/babel.config.cjs delete mode 100644 assignments/config-files/jest.config.js create mode 100644 assignments/hackyourtemperature/.env create mode 100644 week2/practice-exercises/1-joke-api/package.json create mode 100644 week2/prep-exercises/1-blog-API/MamaFile create mode 100644 week2/prep-exercises/1-blog-API/blog.controller.js create mode 100644 week2/prep-exercises/1-blog-API/services/blog.serice.js diff --git a/assignments/config-files/babel.config.cjs b/assignments/config-files/babel.config.cjs deleted file mode 100644 index fbb629af6..000000000 --- a/assignments/config-files/babel.config.cjs +++ /dev/null @@ -1,13 +0,0 @@ -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/jest.config.js b/assignments/config-files/jest.config.js deleted file mode 100644 index 19ba9649e..000000000 --- a/assignments/config-files/jest.config.js +++ /dev/null @@ -1,8 +0,0 @@ -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/.env b/assignments/hackyourtemperature/.env new file mode 100644 index 000000000..c0c68b1ca --- /dev/null +++ b/assignments/hackyourtemperature/.env @@ -0,0 +1 @@ +PORT=3000 \ No newline at end of file diff --git a/week2/practice-exercises/1-joke-api/package.json b/week2/practice-exercises/1-joke-api/package.json new file mode 100644 index 000000000..b07667de8 --- /dev/null +++ b/week2/practice-exercises/1-joke-api/package.json @@ -0,0 +1,13 @@ +{ + "name": "1-joke-api", + "version": "1.0.0", + "description": "Did you know that there is an API for Chuck Norris jokes? That's incredible, right!?", + "main": "script.js", + "type": "module", + "scripts": { + + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/week2/practice-exercises/1-joke-api/script.js b/week2/practice-exercises/1-joke-api/script.js index be4d84612..fcefd312c 100644 --- a/week2/practice-exercises/1-joke-api/script.js +++ b/week2/practice-exercises/1-joke-api/script.js @@ -1,18 +1,12 @@ -/** - * 1. Chuck Norris programs do not accept input - * - * `GET` a random joke inside the function, using the API: http://www.icndb.com/api/ - * (use `node-fetch`) and print it to the console. - * Make use of `async/await` and `try/catch` - * - * Hints - * - To install node dependencies you should first initialize npm - * - Print the entire response to the console to see how it is structured. - */ - -function printChuckNorrisJoke() { - // YOUR CODE GOES IN HERE - +async function printChuckNorrisJoke() { + const response = await fetch("https://api.chucknorris.io/jokes/random"); + if (!response.ok) throw new Error("Failed to fetch data"); + const data = await response.json(); + console.log(data.value); } -printChuckNorrisJoke(); \ No newline at end of file +try { + await printChuckNorrisJoke(); +} catch (error) { + console.error(error.message); +} diff --git a/week2/prep-exercises/1-blog-API/MamaFile b/week2/prep-exercises/1-blog-API/MamaFile new file mode 100644 index 000000000..e594d4672 --- /dev/null +++ b/week2/prep-exercises/1-blog-API/MamaFile @@ -0,0 +1 @@ +Vnature \ No newline at end of file diff --git a/week2/prep-exercises/1-blog-API/blog.controller.js b/week2/prep-exercises/1-blog-API/blog.controller.js new file mode 100644 index 000000000..3934bbeef --- /dev/null +++ b/week2/prep-exercises/1-blog-API/blog.controller.js @@ -0,0 +1,84 @@ +import { Router } from "express"; +import fs from "node:fs/promises"; + +const router = Router(); + +//create new file with title and content +router.post("/", async (req, res, next) => { + const title = req.body.title?.trim(); + const content = req.body.content?.trim(); + + if (!title) { + return res.status(400).send("title is required"); + } + + if (!content) { + return res.status(400).send("content is required"); + } + + try { + await fs.writeFile(title, content, "utf-8"); + res.status(201).send("ok"); + } catch (error) { + next(error); + } +}); +//update title and content +router.put("/:title", async (req, res, next) => { + const title = req.params.title; + let { content, title: newTitle } = req.body; + + if (!newTitle?.trim()) { + newTitle = title; + } + + if (!content?.trim()) { + return res + .status(400) + .json({ message: "Content is required and cannot be empty" }); + } + + try { + await fs.access(title); + await fs.rename(title, newTitle); + await fs.writeFile(newTitle, content); + res + .status(200) + .json({ message: "File content updated successfully", file: newTitle }); + } catch (error) { + next(error); + } +}); + +//delete file +router.delete("/:title", async (req, res, next) => { + const title = req.params.title; + try { + await fs.access(title); + await fs.unlink(title); + res.status(200).json({ message: "File deleted successfully" }); + } catch (err) { + const error = new Error(`Cannot delete file: ${err.message}`); + error.status = 400; + next(error); + } +}); + +//get file content +router.get("/:title", async (req, res, next) => { + const fileTitle = req.params.title; + try { + await fs.access(fileTitle); + const file = await fs.readFile(fileTitle, "utf-8"); + return res.status(200).send(file); + } catch (err) { + const error = new Error( + `Could find file with name ${fileTitle}`, + err.message + ); + error.status = 404; + next(error); + } +}); + +export default router; diff --git a/week2/prep-exercises/1-blog-API/package.json b/week2/prep-exercises/1-blog-API/package.json index d89c4bd76..3094d3747 100644 --- a/week2/prep-exercises/1-blog-API/package.json +++ b/week2/prep-exercises/1-blog-API/package.json @@ -3,9 +3,9 @@ "version": "1.0.0", "description": "", "main": "server.js", + "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "node server.js" + "start": "node --watch server.js" }, "author": "", "license": "ISC", diff --git a/week2/prep-exercises/1-blog-API/server.js b/week2/prep-exercises/1-blog-API/server.js index 3f615e8f5..115022ae1 100644 --- a/week2/prep-exercises/1-blog-API/server.js +++ b/week2/prep-exercises/1-blog-API/server.js @@ -1,10 +1,18 @@ -const express = require('express') +import express from "express"; +import router from "./blog.controller.js"; + const app = express(); - - -// YOUR CODE GOES IN HERE -app.get('/', function (req, res) { - res.send('Hello World') -}) - -app.listen(3000) \ No newline at end of file +const PORT = 3000; + +function main() { + app.use(express.json()); + app.use(express.urlencoded({ extended: false })); + + app.use("/api/blogs", router); + + app.listen(PORT, () => { + console.log(`Server is running at port http://127.0.0.1:${PORT}`); + }); +} + +main(); diff --git a/week2/prep-exercises/1-blog-API/services/blog.serice.js b/week2/prep-exercises/1-blog-API/services/blog.serice.js new file mode 100644 index 000000000..47729157e --- /dev/null +++ b/week2/prep-exercises/1-blog-API/services/blog.serice.js @@ -0,0 +1,5 @@ +export class BlogService { + + + +} \ No newline at end of file From 475c58de21a07bc2c76c8d45ac0bf05e93aaed0f Mon Sep 17 00:00:00 2001 From: Yana Seniuk Date: Fri, 2 May 2025 19:01:31 +0200 Subject: [PATCH 08/13] Refactor: added error handler --- .../src/controllers/weather.controller.js | 11 +++++++++ .../src/middlewares/error.middleware.js | 5 ++++ .../src/middlewares/notFound.middleware.js | 5 ++++ .../src/middlewares/validateBody.js | 9 ++++++++ .../src/routes/temperature.routes.js | 8 +++++++ assignments/hackyourtemperature/src/server.js | 21 ----------------- .../src/temperature.controller.js | 23 ------------------- .../src/temperature.service.js | 5 ---- 8 files changed, 38 insertions(+), 49 deletions(-) create mode 100644 assignments/hackyourtemperature/src/controllers/weather.controller.js create mode 100644 assignments/hackyourtemperature/src/middlewares/error.middleware.js create mode 100644 assignments/hackyourtemperature/src/middlewares/notFound.middleware.js create mode 100644 assignments/hackyourtemperature/src/middlewares/validateBody.js create mode 100644 assignments/hackyourtemperature/src/routes/temperature.routes.js delete mode 100644 assignments/hackyourtemperature/src/server.js delete mode 100644 assignments/hackyourtemperature/src/temperature.controller.js delete mode 100644 assignments/hackyourtemperature/src/temperature.service.js diff --git a/assignments/hackyourtemperature/src/controllers/weather.controller.js b/assignments/hackyourtemperature/src/controllers/weather.controller.js new file mode 100644 index 000000000..0cd25c2b2 --- /dev/null +++ b/assignments/hackyourtemperature/src/controllers/weather.controller.js @@ -0,0 +1,11 @@ +export function getCityName(req, res, next) { + const { cityName } = req.body; + + if (!cityName?.trim()) { + const error = new Error("Required parameter 'cityName' is missing"); + error.status = 400; + return next(error); + } + + res.status(200).json({ cityName }); +} diff --git a/assignments/hackyourtemperature/src/middlewares/error.middleware.js b/assignments/hackyourtemperature/src/middlewares/error.middleware.js new file mode 100644 index 000000000..dcd23d2af --- /dev/null +++ b/assignments/hackyourtemperature/src/middlewares/error.middleware.js @@ -0,0 +1,5 @@ +export function errorHandler(err, req, res, next) { + const statusCode = err.status || 500; + const message = err.message || "Something went wrong"; + res.status(statusCode).json({ message }); +} diff --git a/assignments/hackyourtemperature/src/middlewares/notFound.middleware.js b/assignments/hackyourtemperature/src/middlewares/notFound.middleware.js new file mode 100644 index 000000000..46b644b6f --- /dev/null +++ b/assignments/hackyourtemperature/src/middlewares/notFound.middleware.js @@ -0,0 +1,5 @@ +export function notFound(req, res, next) { + const error = new Error(`Not found`); + error.status = 404; + next(error); +} diff --git a/assignments/hackyourtemperature/src/middlewares/validateBody.js b/assignments/hackyourtemperature/src/middlewares/validateBody.js new file mode 100644 index 000000000..a988d0bcd --- /dev/null +++ b/assignments/hackyourtemperature/src/middlewares/validateBody.js @@ -0,0 +1,9 @@ +export function validateBody(req, res, next) { + if (!req.body || Object.keys(req.body).length === 0) { + const error = new Error("Request body is missing"); + error.status = 400; + return next(error); + } + + next(); +} diff --git a/assignments/hackyourtemperature/src/routes/temperature.routes.js b/assignments/hackyourtemperature/src/routes/temperature.routes.js new file mode 100644 index 000000000..9c6ebf277 --- /dev/null +++ b/assignments/hackyourtemperature/src/routes/temperature.routes.js @@ -0,0 +1,8 @@ +import { Router } from "express"; +import { validateBody } from "../middlewares/validateBody.js"; +import { getCityName } from "../controllers/weather.controller.js"; +const router = Router(); + +router.post("/", validateBody, getCityName); + +export default router; diff --git a/assignments/hackyourtemperature/src/server.js b/assignments/hackyourtemperature/src/server.js deleted file mode 100644 index 62426d8cd..000000000 --- a/assignments/hackyourtemperature/src/server.js +++ /dev/null @@ -1,21 +0,0 @@ -import express from "express"; -import { temperatureRouter } from "./temperature.controller.js"; - -const app = express(); -const PORT = process.env.PORT || 3000; - -function main() { - app.use(express.json()); - - app.use("/api", temperatureRouter); - - app.use((req, res) => { - res.status(404).json({ message: "Not found" }); - }); - - app.listen(PORT, () => { - console.log(`Server is running at http://127.0.0.1:${PORT}`); - }); -} - -main(); diff --git a/assignments/hackyourtemperature/src/temperature.controller.js b/assignments/hackyourtemperature/src/temperature.controller.js deleted file mode 100644 index 4125dc86d..000000000 --- a/assignments/hackyourtemperature/src/temperature.controller.js +++ /dev/null @@ -1,23 +0,0 @@ -import { Router } from "express"; -import { TemperatureService } from "./temperature.service.js"; -const temperatureRouter = Router(); -const temperatureService = new TemperatureService(); - -temperatureRouter.get("/", (req, res) => { - res.status(200).send("hello from backend to frontend!"); -}); - -temperatureRouter.post("/weather", (req, res) => { - const { cityName } = req.body; - - if (!cityName) - return res.status(400).json({ - error: "Bad Request", - message: "Required parameter 'cityName' is missing", - }); - - const result = temperatureService.getCityName(cityName); - res.status(200).json(result); -}); - -export { temperatureRouter }; diff --git a/assignments/hackyourtemperature/src/temperature.service.js b/assignments/hackyourtemperature/src/temperature.service.js deleted file mode 100644 index e5ac389b4..000000000 --- a/assignments/hackyourtemperature/src/temperature.service.js +++ /dev/null @@ -1,5 +0,0 @@ -export class TemperatureService { - getCityName(cityName) { - return cityName; - } -} From 5fa752591fd876e45dafff1599a066cb570be4b7 Mon Sep 17 00:00:00 2001 From: Yana Seniuk Date: Fri, 2 May 2025 19:04:46 +0200 Subject: [PATCH 09/13] Include server.js --- assignments/hackyourtemperature/server.js | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 assignments/hackyourtemperature/server.js diff --git a/assignments/hackyourtemperature/server.js b/assignments/hackyourtemperature/server.js new file mode 100644 index 000000000..b8553a956 --- /dev/null +++ b/assignments/hackyourtemperature/server.js @@ -0,0 +1,33 @@ +import express from "express"; +import weatherRouter from "./src/routes/temperature.routes.js"; + +import { notFound } from "./src/middlewares/notFound.middleware.js"; +import { errorHandler } from "./src/middlewares/error.middleware.js"; + +const app = express(); +const PORT = process.env.PORT || 3000; + +function main() { + app.use(express.json()); + app.use(express.urlencoded({ extended: false })); + + //check server works + app.get("/", (req, res) => { + res.status(200).send("hello from backend to frontend!"); + }); + + //Api Routes + app.use("/api/weather", weatherRouter); + + //non-exist path + app.use(notFound); + + //error handling + app.use(errorHandler); + + app.listen(PORT, () => { + console.log(`Server is running at http://127.0.0.1:${PORT}`); + }); +} + +main(); From 03c1982f943baa4bff376f3d9f3ef59076f12716 Mon Sep 17 00:00:00 2001 From: Yana Seniuk Date: Fri, 2 May 2025 19:06:33 +0200 Subject: [PATCH 10/13] include server.js --- assignments/hackyourtemperature/package.json | 2 +- assignments/hackyourtemperature/server.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/assignments/hackyourtemperature/package.json b/assignments/hackyourtemperature/package.json index 99fc6585a..5cc9a66c3 100644 --- a/assignments/hackyourtemperature/package.json +++ b/assignments/hackyourtemperature/package.json @@ -5,7 +5,7 @@ "main": "server.js", "type": "module", "scripts": { - "start": "node --watch --env-file=.env src/server.js" + "start": "node --watch --env-file=.env server.js" }, "author": "yanaesher", "license": "ISC", diff --git a/assignments/hackyourtemperature/server.js b/assignments/hackyourtemperature/server.js index b8553a956..7c9a4f37d 100644 --- a/assignments/hackyourtemperature/server.js +++ b/assignments/hackyourtemperature/server.js @@ -21,7 +21,7 @@ function main() { //non-exist path app.use(notFound); - + //error handling app.use(errorHandler); From bcfc24d6d7511609aff4312167e1cc80620564ac Mon Sep 17 00:00:00 2001 From: yanaesher Date: Sun, 4 May 2025 20:48:10 +0200 Subject: [PATCH 11/13] feat: solve week2 --- .../hackyourtemperature/__tests__/app.test.js | 62 +++++++++++++++++++ assignments/hackyourtemperature/app.js | 26 ++++++++ .../hackyourtemperature/babel.config.cjs | 12 ++++ .../hackyourtemperature/jest.config.js | 8 +++ assignments/hackyourtemperature/package.json | 15 +++-- assignments/hackyourtemperature/server.js | 24 +------ .../hackyourtemperature/src/constants.js | 2 + .../src/controllers/weather.controller.js | 18 +++++- assignments/hackyourtemperature/src/keys.js | 1 + .../src/middlewares/error.middleware.js | 3 +- .../src/middlewares/validateBody.js | 2 +- .../src/routes/temperature.routes.js | 4 +- .../src/services/weather.service.js | 10 +++ .../src/utils/fetchHelper.js | 11 ++++ 14 files changed, 164 insertions(+), 34 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/src/constants.js create mode 100644 assignments/hackyourtemperature/src/keys.js create mode 100644 assignments/hackyourtemperature/src/services/weather.service.js create mode 100644 assignments/hackyourtemperature/src/utils/fetchHelper.js diff --git a/assignments/hackyourtemperature/__tests__/app.test.js b/assignments/hackyourtemperature/__tests__/app.test.js new file mode 100644 index 000000000..b5670999d --- /dev/null +++ b/assignments/hackyourtemperature/__tests__/app.test.js @@ -0,0 +1,62 @@ +import app from "../app.js"; +import supertest from "supertest"; + +const request = supertest(app); + +describe("GET /", () => { + it("Should return text 'hello from backend to frontend'", async () => { + const response = await request.get("/"); + + expect(response.text).toBe("hello from backend to frontend!"); + expect(response.status).toBe(200); + }); +}); + +describe("POST /api/weather", () => { + it("Should response JSON with cityName and temperature properties", async () => { + const response = await request.post("/api/weather").send({ + cityName: "Amsterdam", + }); + + expect(response.status).toBe(200); + expect(response.body).toHaveProperty("cityName"); + expect(response.body).toHaveProperty("temperature"); + }); + + it("Should response JSON with cityName not found", async () => { + const cityName = "andromeda"; + const response = await request.post("/api/weather").send({ + cityName: `${cityName}`, + }); + expect(response.status).toBe(404); + expect(response.body).toEqual({ error: `${cityName} not found` }); + }); + + it("Should response 'Request body is missing'", async () => { + const response = await request.post("/api/weather"); + + expect(response.status).toBe(400); + expect(response.body.error).toMatch(/request body is missing/i); + }); + + it("Should response 'Required parameter 'cityName' is missing'", async () => { + const response = await request + .post("/api/weather") + .send({ cityName: " " }); + + expect(response.status).toBe(400); + expect(response.body).toEqual({ + error: "Required parameter 'cityName' is missing", + }); + }); +}); + +describe("Unknown routes", () => { + it("Should respond with 404 for undefined path", async () => { + const response = await request.get("/non-existing-path"); + + expect(response.status).toBe(404); + expect(response.body).toHaveProperty("error"); + expect(response.body.error).toMatch(/not found/i); + }); +}); diff --git a/assignments/hackyourtemperature/app.js b/assignments/hackyourtemperature/app.js new file mode 100644 index 000000000..e3a7d6b53 --- /dev/null +++ b/assignments/hackyourtemperature/app.js @@ -0,0 +1,26 @@ +import express from "express"; +import weatherRouter from "./src/routes/temperature.routes.js"; + +import { notFound } from "./src/middlewares/notFound.middleware.js"; +import { errorHandler } from "./src/middlewares/error.middleware.js"; + +const app = express(); + +app.use(express.json()); +app.use(express.urlencoded({ extended: false })); + +//check server works +app.get("/", (req, res) => { + res.status(200).send("hello from backend to frontend!"); +}); + +//Api Routes +app.use("/api/weather", weatherRouter); + +//non-exist path +app.use(notFound); + +//error handling +app.use(errorHandler); + +export default app; diff --git a/assignments/hackyourtemperature/babel.config.cjs b/assignments/hackyourtemperature/babel.config.cjs new file mode 100644 index 000000000..392abb66d --- /dev/null +++ b/assignments/hackyourtemperature/babel.config.cjs @@ -0,0 +1,12 @@ +module.exports = { + presets: [ + [ + "@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 index 5cc9a66c3..44ccded93 100644 --- a/assignments/hackyourtemperature/package.json +++ b/assignments/hackyourtemperature/package.json @@ -3,15 +3,20 @@ "version": "1.0.0", "description": "", "main": "server.js", - "type": "module", + "type": "module", "scripts": { - "start": "node --watch --env-file=.env server.js" + "start": "node --watch --env-file=.env server.js", + "test": "jest" }, "author": "yanaesher", "license": "ISC", + "dependencies": { + "express": "^5.1.0" + }, "devDependencies": { - "express": "^5.1.0", - "express-handlebars": "^8.0.3", - "node-fetch": "^3.3.2" + "@babel/preset-env": "^7.27.1", + "babel-jest": "^29.7.0", + "jest": "^29.7.0", + "supertest": "^7.1.0" } } diff --git a/assignments/hackyourtemperature/server.js b/assignments/hackyourtemperature/server.js index 7c9a4f37d..80205b40c 100644 --- a/assignments/hackyourtemperature/server.js +++ b/assignments/hackyourtemperature/server.js @@ -1,30 +1,8 @@ -import express from "express"; -import weatherRouter from "./src/routes/temperature.routes.js"; +import app from "./app.js"; -import { notFound } from "./src/middlewares/notFound.middleware.js"; -import { errorHandler } from "./src/middlewares/error.middleware.js"; - -const app = express(); const PORT = process.env.PORT || 3000; function main() { - app.use(express.json()); - app.use(express.urlencoded({ extended: false })); - - //check server works - app.get("/", (req, res) => { - res.status(200).send("hello from backend to frontend!"); - }); - - //Api Routes - app.use("/api/weather", weatherRouter); - - //non-exist path - app.use(notFound); - - //error handling - app.use(errorHandler); - app.listen(PORT, () => { console.log(`Server is running at http://127.0.0.1:${PORT}`); }); diff --git a/assignments/hackyourtemperature/src/constants.js b/assignments/hackyourtemperature/src/constants.js new file mode 100644 index 000000000..78fbf616e --- /dev/null +++ b/assignments/hackyourtemperature/src/constants.js @@ -0,0 +1,2 @@ +export const BASE_WEATHER_URL = + "https://api.openweathermap.org/data/2.5/weather"; diff --git a/assignments/hackyourtemperature/src/controllers/weather.controller.js b/assignments/hackyourtemperature/src/controllers/weather.controller.js index 0cd25c2b2..a22188f28 100644 --- a/assignments/hackyourtemperature/src/controllers/weather.controller.js +++ b/assignments/hackyourtemperature/src/controllers/weather.controller.js @@ -1,11 +1,25 @@ -export function getCityName(req, res, next) { +import { fetchCurrentWeather } from "../services/weather.service.js"; + +export async function getCityCurrentWeather(req, res, next) { const { cityName } = req.body; if (!cityName?.trim()) { const error = new Error("Required parameter 'cityName' is missing"); error.status = 400; + return next(error); } - res.status(200).json({ cityName }); + try { + const { name, main } = await fetchCurrentWeather(cityName); + res.status(200).json({ cityName: name, temperature: main.temp }); + } catch (err) { + if (err.status === 404) { + const notFoundMessage = new Error(`${cityName} not found`); + notFoundMessage.status = 404; + return next(notFoundMessage); + } + + next(err); + } } diff --git a/assignments/hackyourtemperature/src/keys.js b/assignments/hackyourtemperature/src/keys.js new file mode 100644 index 000000000..b10003544 --- /dev/null +++ b/assignments/hackyourtemperature/src/keys.js @@ -0,0 +1 @@ +export const API_WEATHER_KEY = "007dd5ca6a8f91773d26bde4f0099937"; diff --git a/assignments/hackyourtemperature/src/middlewares/error.middleware.js b/assignments/hackyourtemperature/src/middlewares/error.middleware.js index dcd23d2af..d252b51e2 100644 --- a/assignments/hackyourtemperature/src/middlewares/error.middleware.js +++ b/assignments/hackyourtemperature/src/middlewares/error.middleware.js @@ -1,5 +1,6 @@ export function errorHandler(err, req, res, next) { const statusCode = err.status || 500; const message = err.message || "Something went wrong"; - res.status(statusCode).json({ message }); + + res.status(statusCode).json({ error: message }); } diff --git a/assignments/hackyourtemperature/src/middlewares/validateBody.js b/assignments/hackyourtemperature/src/middlewares/validateBody.js index a988d0bcd..823ecc9e9 100644 --- a/assignments/hackyourtemperature/src/middlewares/validateBody.js +++ b/assignments/hackyourtemperature/src/middlewares/validateBody.js @@ -1,5 +1,5 @@ export function validateBody(req, res, next) { - if (!req.body || Object.keys(req.body).length === 0) { + if (!req.body) { const error = new Error("Request body is missing"); error.status = 400; return next(error); diff --git a/assignments/hackyourtemperature/src/routes/temperature.routes.js b/assignments/hackyourtemperature/src/routes/temperature.routes.js index 9c6ebf277..4435abc9f 100644 --- a/assignments/hackyourtemperature/src/routes/temperature.routes.js +++ b/assignments/hackyourtemperature/src/routes/temperature.routes.js @@ -1,8 +1,8 @@ import { Router } from "express"; import { validateBody } from "../middlewares/validateBody.js"; -import { getCityName } from "../controllers/weather.controller.js"; +import { getCityCurrentWeather } from "../controllers/weather.controller.js"; const router = Router(); -router.post("/", validateBody, getCityName); +router.post("/", validateBody, getCityCurrentWeather); export default router; diff --git a/assignments/hackyourtemperature/src/services/weather.service.js b/assignments/hackyourtemperature/src/services/weather.service.js new file mode 100644 index 000000000..5a17d8231 --- /dev/null +++ b/assignments/hackyourtemperature/src/services/weather.service.js @@ -0,0 +1,10 @@ +import { fetchData } from "../utils/fetchHelper.js"; +import { API_WEATHER_KEY } from "../keys.js"; +import { BASE_WEATHER_URL } from "../constants.js"; + +export async function fetchCurrentWeather(cityName) { + const url = `${BASE_WEATHER_URL}?q=${cityName}&appid=${API_WEATHER_KEY}&units=metric`; + + const weatherData = await fetchData(url); + return weatherData; +} diff --git a/assignments/hackyourtemperature/src/utils/fetchHelper.js b/assignments/hackyourtemperature/src/utils/fetchHelper.js new file mode 100644 index 000000000..64974a855 --- /dev/null +++ b/assignments/hackyourtemperature/src/utils/fetchHelper.js @@ -0,0 +1,11 @@ +export async function fetchData(url) { + const response = await fetch(url); + + if (!response.ok) { + const error = new Error(`Failed to fetch: ${response.status}`); + error.status = response.status; + throw error; + } + + return response.json(); +} From 97363357939a0a6d6820498ea2eea188c8b3d86d Mon Sep 17 00:00:00 2001 From: yanaesher Date: Mon, 5 May 2025 23:11:52 +0200 Subject: [PATCH 12/13] refactor: moved server to src, changes json --- .../hackyourtemperature/__tests__/app.test.js | 2 +- assignments/hackyourtemperature/package.json | 3 ++- assignments/hackyourtemperature/server.js | 11 ----------- assignments/hackyourtemperature/{ => src}/app.js | 14 ++++---------- ...emperature.routes.js => temperature.route.js} | 16 ++++++++-------- assignments/hackyourtemperature/src/server.js | 7 +++++++ 6 files changed, 22 insertions(+), 31 deletions(-) delete mode 100644 assignments/hackyourtemperature/server.js rename assignments/hackyourtemperature/{ => src}/app.js (54%) rename assignments/hackyourtemperature/src/routes/{temperature.routes.js => temperature.route.js} (97%) create mode 100644 assignments/hackyourtemperature/src/server.js diff --git a/assignments/hackyourtemperature/__tests__/app.test.js b/assignments/hackyourtemperature/__tests__/app.test.js index b5670999d..e39456279 100644 --- a/assignments/hackyourtemperature/__tests__/app.test.js +++ b/assignments/hackyourtemperature/__tests__/app.test.js @@ -1,4 +1,4 @@ -import app from "../app.js"; +import app from "../src/app.js"; import supertest from "supertest"; const request = supertest(app); diff --git a/assignments/hackyourtemperature/package.json b/assignments/hackyourtemperature/package.json index 44ccded93..696127971 100644 --- a/assignments/hackyourtemperature/package.json +++ b/assignments/hackyourtemperature/package.json @@ -5,7 +5,8 @@ "main": "server.js", "type": "module", "scripts": { - "start": "node --watch --env-file=.env server.js", + "start": "node src/server.js", + "dev": "node --watch --env-file=.env src/server.js", "test": "jest" }, "author": "yanaesher", diff --git a/assignments/hackyourtemperature/server.js b/assignments/hackyourtemperature/server.js deleted file mode 100644 index 80205b40c..000000000 --- a/assignments/hackyourtemperature/server.js +++ /dev/null @@ -1,11 +0,0 @@ -import app from "./app.js"; - -const PORT = process.env.PORT || 3000; - -function main() { - app.listen(PORT, () => { - console.log(`Server is running at http://127.0.0.1:${PORT}`); - }); -} - -main(); diff --git a/assignments/hackyourtemperature/app.js b/assignments/hackyourtemperature/src/app.js similarity index 54% rename from assignments/hackyourtemperature/app.js rename to assignments/hackyourtemperature/src/app.js index e3a7d6b53..1a7d7a9fa 100644 --- a/assignments/hackyourtemperature/app.js +++ b/assignments/hackyourtemperature/src/app.js @@ -1,26 +1,20 @@ import express from "express"; -import weatherRouter from "./src/routes/temperature.routes.js"; +import weatherRouter from "./routes/temperature.route.js"; -import { notFound } from "./src/middlewares/notFound.middleware.js"; -import { errorHandler } from "./src/middlewares/error.middleware.js"; +import { notFound } from "./middlewares/notFound.middleware.js"; +import { errorHandler } from "./middlewares/error.middleware.js"; const app = express(); app.use(express.json()); app.use(express.urlencoded({ extended: false })); -//check server works +app.use("/api/weather", weatherRouter); app.get("/", (req, res) => { res.status(200).send("hello from backend to frontend!"); }); -//Api Routes -app.use("/api/weather", weatherRouter); - -//non-exist path app.use(notFound); - -//error handling app.use(errorHandler); export default app; diff --git a/assignments/hackyourtemperature/src/routes/temperature.routes.js b/assignments/hackyourtemperature/src/routes/temperature.route.js similarity index 97% rename from assignments/hackyourtemperature/src/routes/temperature.routes.js rename to assignments/hackyourtemperature/src/routes/temperature.route.js index 4435abc9f..165d132f2 100644 --- a/assignments/hackyourtemperature/src/routes/temperature.routes.js +++ b/assignments/hackyourtemperature/src/routes/temperature.route.js @@ -1,8 +1,8 @@ -import { Router } from "express"; -import { validateBody } from "../middlewares/validateBody.js"; -import { getCityCurrentWeather } from "../controllers/weather.controller.js"; -const router = Router(); - -router.post("/", validateBody, getCityCurrentWeather); - -export default router; +import { Router } from "express"; +import { validateBody } from "../middlewares/validateBody.js"; +import { getCityCurrentWeather } from "../controllers/weather.controller.js"; +const router = Router(); + +router.post("/", validateBody, getCityCurrentWeather); + +export default router; diff --git a/assignments/hackyourtemperature/src/server.js b/assignments/hackyourtemperature/src/server.js new file mode 100644 index 000000000..7adc49ba2 --- /dev/null +++ b/assignments/hackyourtemperature/src/server.js @@ -0,0 +1,7 @@ +import app from "./app.js"; + +const PORT = process.env.PORT || 3000; + +app.listen(PORT, () => { + console.log(`Server is running at http://127.0.0.1:${PORT}`); +}); From fc8904eb0ed56a5f69def11f515a67e0b3c7599a Mon Sep 17 00:00:00 2001 From: yanaesher Date: Tue, 6 May 2025 00:49:14 +0200 Subject: [PATCH 13/13] refactor: change GET/ to GET/api --- assignments/hackyourtemperature/src/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignments/hackyourtemperature/src/app.js b/assignments/hackyourtemperature/src/app.js index 1a7d7a9fa..40607eb2c 100644 --- a/assignments/hackyourtemperature/src/app.js +++ b/assignments/hackyourtemperature/src/app.js @@ -10,7 +10,7 @@ app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use("/api/weather", weatherRouter); -app.get("/", (req, res) => { +app.get("/api", (req, res) => { res.status(200).send("hello from backend to frontend!"); });