From ac9ef8f220d2ad486021344325902e59f1eaae50 Mon Sep 17 00:00:00 2001 From: Dmytro-Doronin Date: Tue, 28 Oct 2025 13:58:02 +0100 Subject: [PATCH 1/2] NodeJs week1 assignment --- assignments/app.js | 8 ++++++++ assignments/controllers/mainController.js | 14 ++++++++++++++ assignments/index.js | 14 ++++++++++++++ assignments/package.json | 23 +++++++++++++++++++++++ assignments/routes/routes.js | 7 +++++++ 5 files changed, 66 insertions(+) create mode 100644 assignments/app.js create mode 100644 assignments/controllers/mainController.js create mode 100644 assignments/index.js create mode 100644 assignments/package.json create mode 100644 assignments/routes/routes.js diff --git a/assignments/app.js b/assignments/app.js new file mode 100644 index 000000000..aceca6e98 --- /dev/null +++ b/assignments/app.js @@ -0,0 +1,8 @@ +import express from "express"; +import {generalRouter} from "./routes/routes.js"; + +export const app = express() + +app.use(express.json()) + +app.use('/api', generalRouter) \ No newline at end of file diff --git a/assignments/controllers/mainController.js b/assignments/controllers/mainController.js new file mode 100644 index 000000000..60b01325d --- /dev/null +++ b/assignments/controllers/mainController.js @@ -0,0 +1,14 @@ +export const returnHelloText = (req, res) => { + return res.status(200).json({ message: 'hello from backend to frontend!' }) +} + + +export const getCityWeather = (req, res) => { + const {cityName} = req.body + + if (!cityName) { + return res.status(400).json({ error: 'cityName is required' }) + } + + return res.status(200).json({ cityName }) +} \ No newline at end of file diff --git a/assignments/index.js b/assignments/index.js new file mode 100644 index 000000000..3b2cbab8e --- /dev/null +++ b/assignments/index.js @@ -0,0 +1,14 @@ +import {app} from "./app.js"; + +const PORT = 3000; + +const startApp = async () => { + try { + app.listen(PORT, () => { + console.log(`Server listening on port ${PORT}`) + }) + } catch (e) { + console.log(e) + } +} +startApp() \ No newline at end of file diff --git a/assignments/package.json b/assignments/package.json new file mode 100644 index 000000000..e86ba319f --- /dev/null +++ b/assignments/package.json @@ -0,0 +1,23 @@ +{ + "name": "assignments", + "version": "1.0.0", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "dev": "nodemon --inspect index.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "express": "5.1.0", + "express-handlebars": "8.0.1", + "node-fetch": "3.3.2" + }, + "devDependencies": { + "nodemon": "3.1.10" + } +} + diff --git a/assignments/routes/routes.js b/assignments/routes/routes.js new file mode 100644 index 000000000..4a1c5ac2c --- /dev/null +++ b/assignments/routes/routes.js @@ -0,0 +1,7 @@ +import {Router} from "express"; +import {getCityWeather, returnHelloText} from "../controllers/mainController.js"; + +export const generalRouter = Router() + +generalRouter.get('/', returnHelloText) +generalRouter.post('/weather', getCityWeather) From 9a22755f854b5fabf15bf41aebc091ff48c83533 Mon Sep 17 00:00:00 2001 From: Dmytro-Doronin Date: Tue, 28 Oct 2025 13:59:26 +0100 Subject: [PATCH 2/2] NodeJs week1 assignment --- assignments/app.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/assignments/app.js b/assignments/app.js index aceca6e98..04dd5a271 100644 --- a/assignments/app.js +++ b/assignments/app.js @@ -2,7 +2,5 @@ import express from "express"; import {generalRouter} from "./routes/routes.js"; export const app = express() - app.use(express.json()) - app.use('/api', generalRouter) \ No newline at end of file