From 2eea9bced0298a60aa93076f117dbfd60ef26a77 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 25 Aug 2026 19:43:31 +0100 Subject: [PATCH 01/11] Create basic chat app --- chat-app/.gitignore | 2 ++ chat-app/backend/package.json | 14 ++++++++++++++ chat-app/backend/server.js | 33 +++++++++++++++++++++++++++++++++ chat-app/frontend/app.js | 19 +++++++++++++++++++ chat-app/frontend/index.html | 19 +++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 chat-app/.gitignore create mode 100644 chat-app/backend/package.json create mode 100644 chat-app/backend/server.js create mode 100644 chat-app/frontend/app.js create mode 100644 chat-app/frontend/index.html diff --git a/chat-app/.gitignore b/chat-app/.gitignore new file mode 100644 index 00000000..ec71af6a --- /dev/null +++ b/chat-app/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +.env* \ No newline at end of file diff --git a/chat-app/backend/package.json b/chat-app/backend/package.json new file mode 100644 index 00000000..6fed2b27 --- /dev/null +++ b/chat-app/backend/package.json @@ -0,0 +1,14 @@ +{ + "name": "backend", + "version": "1.0.0", + "description": "", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "module" +} diff --git a/chat-app/backend/server.js b/chat-app/backend/server.js new file mode 100644 index 00000000..d1136eae --- /dev/null +++ b/chat-app/backend/server.js @@ -0,0 +1,33 @@ +import http from "node:http"; + +const PORT = process.env.PORT || 3002; + +const messages = []; +const httpServer = http.createServer((req, res) => { + + if(req.method === "GET" && req.url === "/messages"){ + res.setHeader("Content-Type", "application/json"); + res.end(JSON.stringify(messages)); + } + // getting the message from user + if (req.method === "POST" && req.url === "/messages") { + let body = ""; + + req.on("data", (chunk) => { + body += chunk; + }); + + req.on("end", () => { + const message = JSON.parse(body); + + messages.push(message); + + res.statusCode = 201; + res.end(); + }); + } +}); + +httpServer.listen(PORT, () => { + console.log(`server is running on http://localhost:${PORT}`); +}); diff --git a/chat-app/frontend/app.js b/chat-app/frontend/app.js new file mode 100644 index 00000000..33ee3ca8 --- /dev/null +++ b/chat-app/frontend/app.js @@ -0,0 +1,19 @@ +// touch html elements: +const messagesContainer = document.getElementById("messages"); +const messageInput = document.getElementById("message-input"); +const sendButton = document.getElementById("send-button"); + +// Get message from server; +async function getMessages() { + const response = await fetch("http://localhost:3002/messages"); + const messages = await response.json(); + + messagesContainer.innerHTML = ""; + + messages.forEach((message) => { + const messageElement = document.createElement("p"); + messageElement.textContent = message.text; + + messagesContainer.appendChild(messageElement); + }); +} \ No newline at end of file diff --git a/chat-app/frontend/index.html b/chat-app/frontend/index.html new file mode 100644 index 00000000..ba7a580b --- /dev/null +++ b/chat-app/frontend/index.html @@ -0,0 +1,19 @@ + + + + + + Simple Chat + + + +

Chat

+ +
+ + + + + + + From 4fa65be5d19db813e5d51cbab4297b76c650f203 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 25 Aug 2026 20:32:14 +0100 Subject: [PATCH 02/11] send button functionality added --- chat-app/backend/server.js | 15 +++++++++++++-- chat-app/frontend/app.js | 23 ++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/chat-app/backend/server.js b/chat-app/backend/server.js index d1136eae..236c600f 100644 --- a/chat-app/backend/server.js +++ b/chat-app/backend/server.js @@ -4,9 +4,19 @@ const PORT = process.env.PORT || 3002; const messages = []; const httpServer = http.createServer((req, res) => { - - if(req.method === "GET" && req.url === "/messages"){ + res.setHeader("Access-Control-Allow-Origin", "*"); + res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); + res.setHeader("Access-Control-Allow-Headers", "Content-Type"); + + if (req.method === "OPTIONS") { + res.statusCode = 204; + res.end(); + return; + } + + if (req.method === "GET" && req.url === "/messages") { res.setHeader("Content-Type", "application/json"); + console.log("Sending messages:", messages); res.end(JSON.stringify(messages)); } // getting the message from user @@ -19,6 +29,7 @@ const httpServer = http.createServer((req, res) => { req.on("end", () => { const message = JSON.parse(body); + console.log("Received message:", message); messages.push(message); diff --git a/chat-app/frontend/app.js b/chat-app/frontend/app.js index 33ee3ca8..c2ef961e 100644 --- a/chat-app/frontend/app.js +++ b/chat-app/frontend/app.js @@ -16,4 +16,25 @@ async function getMessages() { messagesContainer.appendChild(messageElement); }); -} \ No newline at end of file +} + +// Send message event: +sendButton.addEventListener("click", async () => { + const text = messageInput.value; + + if (text.trim() === "") { + return; + } + + await fetch("http://localhost:3002/messages", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ text: text }), + }); + + messageInput.value = ""; + + await getMessages(); +}); From 321dea34016cadc46f8be682fe2a35a05f48e176 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 25 Aug 2026 21:01:46 +0100 Subject: [PATCH 03/11] Switch backend to Express --- chat-app/backend/package-lock.json | 913 +++++++++++++++++++++++++++++ chat-app/backend/package.json | 6 +- chat-app/backend/server.js | 62 +- 3 files changed, 944 insertions(+), 37 deletions(-) create mode 100644 chat-app/backend/package-lock.json diff --git a/chat-app/backend/package-lock.json b/chat-app/backend/package-lock.json new file mode 100644 index 00000000..7d6045e9 --- /dev/null +++ b/chat-app/backend/package-lock.json @@ -0,0 +1,913 @@ +{ + "name": "backend", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "backend", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "cors": "^2.8.6", + "express": "^5.2.1" + } + }, + "node_modules/accepts": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz", + "integrity": "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng==", + "license": "MIT", + "dependencies": { + "mime-types": "^3.0.0", + "negotiator": "^1.0.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/body-parser": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.3.0.tgz", + "integrity": "sha512-2cGmJupaNgg+QUwVLAucDuWuoMZ6EX9iHDRswZ5lsNYEmwPaRknMPCLZz07yTzVq/83p4o/wzbDZbBrTvGGTIw==", + "license": "MIT", + "dependencies": { + "bytes": "^3.1.2", + "content-type": "^2.0.0", + "debug": "^4.4.3", + "http-errors": "^2.0.1", + "iconv-lite": "^0.7.2", + "on-finished": "^2.4.1", + "qs": "^6.15.2", + "raw-body": "^3.0.2", + "type-is": "^2.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/body-parser/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/call-bound": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz", + "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "get-intrinsic": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/content-disposition": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-1.1.0.tgz", + "integrity": "sha512-5jRCH9Z/+DRP7rkvY83B+yGIGX96OYdJmzngqnw2SBSxqCFPd0w2km3s5iawpGX8krnwSGmF0FW5Nhr0Hfai3g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz", + "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie-signature": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.2.2.tgz", + "integrity": "sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg==", + "license": "MIT", + "engines": { + "node": ">=6.6.0" + } + }, + "node_modules/cors": { + "version": "2.8.6", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.6.tgz", + "integrity": "sha512-tJtZBBHA6vjIAaF6EnIaq6laBBP9aq/Y3ouVJjEfoHbRBcHBAHYcMh/w8LDrk2PvIMMq8gmopa5D4V8RmbrxGw==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==", + "license": "MIT" + }, + "node_modules/encodeurl": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", + "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.2.tgz", + "integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==", + "license": "MIT" + }, + "node_modules/etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/express": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/express/-/express-5.2.1.tgz", + "integrity": "sha512-hIS4idWWai69NezIdRt2xFVofaF4j+6INOpJlVOLDO8zXGpUVEVzIYk12UUi2JzjEzWL3IOAxcTubgz9Po0yXw==", + "license": "MIT", + "dependencies": { + "accepts": "^2.0.0", + "body-parser": "^2.2.1", + "content-disposition": "^1.0.0", + "content-type": "^1.0.5", + "cookie": "^0.7.1", + "cookie-signature": "^1.2.1", + "debug": "^4.4.0", + "depd": "^2.0.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "finalhandler": "^2.1.0", + "fresh": "^2.0.0", + "http-errors": "^2.0.0", + "merge-descriptors": "^2.0.0", + "mime-types": "^3.0.0", + "on-finished": "^2.4.1", + "once": "^1.4.0", + "parseurl": "^1.3.3", + "proxy-addr": "^2.0.7", + "qs": "^6.14.0", + "range-parser": "^1.2.1", + "router": "^2.2.0", + "send": "^1.1.0", + "serve-static": "^2.2.0", + "statuses": "^2.0.1", + "type-is": "^2.0.1", + "vary": "^1.1.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/finalhandler": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-2.1.1.tgz", + "integrity": "sha512-S8KoZgRZN+a5rNwqTxlZZePjT/4cnm0ROV70LedRHZ0p8u9fRID0hJUZQpkKLzro8LfmC8sx23bY6tVNxv8pQA==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "on-finished": "^2.4.1", + "parseurl": "^1.3.3", + "statuses": "^2.0.1" + }, + "engines": { + "node": ">= 18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fresh": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-2.0.0.tgz", + "integrity": "sha512-Rx/WycZ60HOaqLKAi6cHRKKI7zxWbJ31MhntmtwMoaTeF7XFH9hhBp8vITaMidfljRQ6eYWCKkaTK+ykVJHP2A==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.4.tgz", + "integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/http-errors": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz", + "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==", + "license": "MIT", + "dependencies": { + "depd": "~2.0.0", + "inherits": "~2.0.4", + "setprototypeof": "~1.2.0", + "statuses": "~2.0.2", + "toidentifier": "~1.0.1" + }, + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/iconv-lite": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.3.tgz", + "integrity": "sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, + "node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/is-promise": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-4.0.0.tgz", + "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==", + "license": "MIT" + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/media-typer": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-1.1.1.tgz", + "integrity": "sha512-yz3xRaG20c6/BOzvYoDaGtPmGscs7YivItZEEqe6GbwNfHuxu9YNmvnEkMzKldAGY4/80pRcQRZSEnhquk9XuQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/merge-descriptors": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-2.0.0.tgz", + "integrity": "sha512-Snk314V5ayFLhp3fkUREub6WtjBfPdCPY1Ln8/8munuLuiYhsABgBVWsozAG+MWMbVEvcdcpbi9R7ww22l9Q3g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/mime-db": { + "version": "1.54.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz", + "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-3.0.2.tgz", + "integrity": "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==", + "license": "MIT", + "dependencies": { + "mime-db": "^1.54.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/negotiator": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.1.0.tgz", + "integrity": "sha512-NMPBRMJgiQHjbd8phG3Vebdx4kZ1H121rbl5IkMqeOsahptB9BKo/d7oJ3zTXqTgagn2bWlNSXkh0QUGM31RYg==", + "license": "MIT", + "dependencies": { + "content-type": "^2.1.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/negotiator/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/object-inspect": { + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", + "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/on-finished": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", + "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "license": "ISC", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/path-to-regexp": { + "version": "8.4.2", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.4.2.tgz", + "integrity": "sha512-qRcuIdP69NPm4qbACK+aDogI5CBDMi1jKe0ry5rSQJz8JVLsC7jV8XpiJjGRLLol3N+R5ihGYcrPLTno6pAdBA==", + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/proxy-addr": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", + "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "license": "MIT", + "dependencies": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/qs": { + "version": "6.15.3", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.15.3.tgz", + "integrity": "sha512-O9gl3zCl5h5blw1KGUzQKhA5oUXSl8rwUIM5o0S3nCXMliSvy5Dzx7/DJcI+SwgICv+IneSZwhBh1oSyEHA71A==", + "license": "BSD-3-Clause", + "dependencies": { + "es-define-property": "^1.0.1", + "side-channel": "^1.1.1" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/range-parser": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.3.0.tgz", + "integrity": "sha512-hek2mFQpPuI4E1BBKrSto+BU3e3x4xuarsbiwr3+lf7p44juvFMV0XFWQAP3xUyqXA4RrXLIoaSUGbSt056ZMw==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/raw-body": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.2.tgz", + "integrity": "sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA==", + "license": "MIT", + "dependencies": { + "bytes": "~3.1.2", + "http-errors": "~2.0.1", + "iconv-lite": "~0.7.0", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/router": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/router/-/router-2.2.0.tgz", + "integrity": "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "depd": "^2.0.0", + "is-promise": "^4.0.0", + "parseurl": "^1.3.3", + "path-to-regexp": "^8.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, + "node_modules/send": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/send/-/send-1.2.1.tgz", + "integrity": "sha512-1gnZf7DFcoIcajTjTwjwuDjzuz4PPcY2StKPlsGAQ1+YH20IRVrBaXSWmdjowTJ6u8Rc01PoYOGHXfP1mYcZNQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.3", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "fresh": "^2.0.0", + "http-errors": "^2.0.1", + "mime-types": "^3.0.2", + "ms": "^2.1.3", + "on-finished": "^2.4.1", + "range-parser": "^1.2.1", + "statuses": "^2.0.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/serve-static": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-2.2.1.tgz", + "integrity": "sha512-xRXBn0pPqQTVQiC8wyQrKs2MOlX24zQ0POGaj0kultvoOCstBQM5yvOhAVSUwOMjQtTvsPWoNCHfPGwaaQJhTw==", + "license": "MIT", + "dependencies": { + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "parseurl": "^1.3.3", + "send": "^1.2.0" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==", + "license": "ISC" + }, + "node_modules/side-channel": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.1.tgz", + "integrity": "sha512-6x6dK6zJdpTzF4sQeNYxwtvBzf6Eg4GtlesS94HOvTudUeyK2WXAaIfmDgsyslYrRBeFIlsi54AYsFGUuhmvrQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4", + "side-channel-list": "^1.0.1", + "side-channel-map": "^1.0.1", + "side-channel-weakmap": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-list": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.1.tgz", + "integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-map": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz", + "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-weakmap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", + "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3", + "side-channel-map": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/statuses": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", + "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "license": "MIT", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/type-is": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.1.0.tgz", + "integrity": "sha512-faYHw0anBbc/kWF3zFTEnxSFOAGUX9GFbOBthvDdLsIlEoWOFOtS0zgCiQYwIskL9iGXZL3kAXD8OoZ4GmMATA==", + "license": "MIT", + "dependencies": { + "content-type": "^2.0.0", + "media-typer": "^1.1.0", + "mime-types": "^3.0.0" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/type-is/node_modules/content-type": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.1.0.tgz", + "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "license": "ISC" + } + } +} diff --git a/chat-app/backend/package.json b/chat-app/backend/package.json index 6fed2b27..2927bd9d 100644 --- a/chat-app/backend/package.json +++ b/chat-app/backend/package.json @@ -10,5 +10,9 @@ "keywords": [], "author": "", "license": "ISC", - "type": "module" + "type": "module", + "dependencies": { + "cors": "^2.8.6", + "express": "^5.2.1" + } } diff --git a/chat-app/backend/server.js b/chat-app/backend/server.js index 236c600f..5b81403e 100644 --- a/chat-app/backend/server.js +++ b/chat-app/backend/server.js @@ -1,44 +1,34 @@ -import http from "node:http"; +// import necessary modules +import express from "express"; +import cors from "cors"; + +const app = express(); + +app.use(cors()); +app.use(express.json()); const PORT = process.env.PORT || 3002; const messages = []; -const httpServer = http.createServer((req, res) => { - res.setHeader("Access-Control-Allow-Origin", "*"); - res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); - res.setHeader("Access-Control-Allow-Headers", "Content-Type"); - - if (req.method === "OPTIONS") { - res.statusCode = 204; - res.end(); - return; - } - - if (req.method === "GET" && req.url === "/messages") { - res.setHeader("Content-Type", "application/json"); - console.log("Sending messages:", messages); - res.end(JSON.stringify(messages)); - } - // getting the message from user - if (req.method === "POST" && req.url === "/messages") { - let body = ""; - - req.on("data", (chunk) => { - body += chunk; - }); - - req.on("end", () => { - const message = JSON.parse(body); - console.log("Received message:", message); - - messages.push(message); - - res.statusCode = 201; - res.end(); - }); - } + +// GET all messages +app.get("/messages", (req, res) => { + console.log("Sending messages:", messages); + + res.json(messages); +}); + +// POST a new message +app.post("/messages", (req, res) => { + const message = req.body; + + console.log("Received message:", message); + + messages.push(message); + + res.status(201).json(message); }); -httpServer.listen(PORT, () => { +app.listen(PORT, () => { console.log(`server is running on http://localhost:${PORT}`); }); From 174c765da99a5c1ebb45aa21ffef67185513657a Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 25 Aug 2026 21:38:57 +0100 Subject: [PATCH 04/11] Docker file added --- chat-app/backend/Dockerfile | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 chat-app/backend/Dockerfile diff --git a/chat-app/backend/Dockerfile b/chat-app/backend/Dockerfile new file mode 100644 index 00000000..0c6b97ee --- /dev/null +++ b/chat-app/backend/Dockerfile @@ -0,0 +1,11 @@ +FROM node:alpine + +ENV NODE_ENV=production + +COPY . /app + +WORKDIR /app + +RUN npm --omit=dev ci + +ENTRYPOINT ["node", "server.js"] \ No newline at end of file From 4de471726a5a38049b394a95ee7f0d5e3411c04d Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 25 Aug 2026 22:21:57 +0100 Subject: [PATCH 05/11] Connect frontend to deployed backend --- chat-app/frontend/app.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/chat-app/frontend/app.js b/chat-app/frontend/app.js index c2ef961e..cec9c960 100644 --- a/chat-app/frontend/app.js +++ b/chat-app/frontend/app.js @@ -5,7 +5,9 @@ const sendButton = document.getElementById("send-button"); // Get message from server; async function getMessages() { - const response = await fetch("http://localhost:3002/messages"); + const response = await fetch( + "https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages", + ); const messages = await response.json(); messagesContainer.innerHTML = ""; @@ -26,15 +28,21 @@ sendButton.addEventListener("click", async () => { return; } - await fetch("http://localhost:3002/messages", { - method: "POST", - headers: { - "Content-Type": "application/json", + await fetch( + "https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages", + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ text: text }), }, - body: JSON.stringify({ text: text }), - }); + ); messageInput.value = ""; await getMessages(); }); + +// Load messages when the page opens +getMessages(); From 51edc3f354145a7d5f5a158f4bcad04060673cfd Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 25 Aug 2026 23:16:32 +0100 Subject: [PATCH 06/11] Add polling for new messages --- chat-app/frontend/app.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/chat-app/frontend/app.js b/chat-app/frontend/app.js index cec9c960..e912e45d 100644 --- a/chat-app/frontend/app.js +++ b/chat-app/frontend/app.js @@ -46,3 +46,6 @@ sendButton.addEventListener("click", async () => { // Load messages when the page opens getMessages(); + +// Ask the server for messages every 2 seconds +setInterval(getMessages, 2000); From 8b201fbf8000d74f9957357a6cd074ddb8c31747 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 26 Aug 2026 17:10:42 +0100 Subject: [PATCH 07/11] Add long polling --- chat-app/backend/server.js | 22 +++++++++++++++++++--- chat-app/frontend/app.js | 13 +++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/chat-app/backend/server.js b/chat-app/backend/server.js index 5b81403e..b7cbd394 100644 --- a/chat-app/backend/server.js +++ b/chat-app/backend/server.js @@ -10,21 +10,37 @@ app.use(express.json()); const PORT = process.env.PORT || 3002; const messages = []; +const callbacksForNewMessages = []; // GET all messages app.get("/messages", (req, res) => { - console.log("Sending messages:", messages); + const since = Number(req.query.since); - res.json(messages); + console.log("Client asked for messages since:", since); + const newMessages = messages.filter((message) => message.id > since); + console.log("Sending messages:", newMessages); + + if (newMessages.length === 0) { + callbacksForNewMessages.push((value) => res.json(value)); + } else { + res.json(newMessages); + } }); // POST a new message app.post("/messages", (req, res) => { - const message = req.body; + const message = { + id: messages.length, + text: req.body.text, + }; console.log("Received message:", message); messages.push(message); + while (callbacksForNewMessages.length > 0) { + const callback = callbacksForNewMessages.pop(); + callback([message]); + } res.status(201).json(message); }); diff --git a/chat-app/frontend/app.js b/chat-app/frontend/app.js index e912e45d..6f86594d 100644 --- a/chat-app/frontend/app.js +++ b/chat-app/frontend/app.js @@ -3,21 +3,23 @@ const messagesContainer = document.getElementById("messages"); const messageInput = document.getElementById("message-input"); const sendButton = document.getElementById("send-button"); +let lastMessageId = -1; + // Get message from server; async function getMessages() { const response = await fetch( - "https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages", + `https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages?since=${lastMessageId}`, ); const messages = await response.json(); - messagesContainer.innerHTML = ""; - messages.forEach((message) => { const messageElement = document.createElement("p"); messageElement.textContent = message.text; messagesContainer.appendChild(messageElement); + lastMessageId = message.id; }); + getMessages(); } // Send message event: @@ -40,12 +42,7 @@ sendButton.addEventListener("click", async () => { ); messageInput.value = ""; - - await getMessages(); }); // Load messages when the page opens getMessages(); - -// Ask the server for messages every 2 seconds -setInterval(getMessages, 2000); From 10e683c66ac056f56605c5f6c5094d4ece32ec54 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 1 Sep 2026 01:19:07 +0100 Subject: [PATCH 08/11] Add like and dislike reactions --- chat-app/backend/server.js | 54 +++++++++++++++++++- chat-app/frontend/app.js | 91 +++++++++++++++++++++++++++++++-- chat-app/frontend/index.html | 18 +++++-- chat-app/frontend/style.css | 99 ++++++++++++++++++++++++++++++++++++ 4 files changed, 253 insertions(+), 9 deletions(-) create mode 100644 chat-app/frontend/style.css diff --git a/chat-app/backend/server.js b/chat-app/backend/server.js index b7cbd394..bd5b15a1 100644 --- a/chat-app/backend/server.js +++ b/chat-app/backend/server.js @@ -29,9 +29,22 @@ app.get("/messages", (req, res) => { // POST a new message app.post("/messages", (req, res) => { + // Validate the request body + if ( + typeof req.body.text !== "string" || + req.body.text.trim() === "" || + typeof req.body.user !== "string" || + req.body.user.trim() === "" + ) { + res.status(400).send("Expected a username and a non-empty text string."); + return; + } + const message = { id: messages.length, - text: req.body.text, + text: req.body.text.trim(), + likes: 0, + dislikes: 0, }; console.log("Received message:", message); @@ -45,6 +58,45 @@ app.post("/messages", (req, res) => { res.status(201).json(message); }); +app.post("/messages/:id/reaction", (req, res) => { + const messageId = Number(req.params.id); + const reaction = req.body.reaction; + + const message = messages.find((message) => message.id === messageId); + + if (!message) { + res.status(404).send("Message not found."); + return; + } + + if (reaction !== "like" && reaction !== "dislike") { + res.status(400).send("Reaction must be like or dislike."); + return; + } + + if (reaction === "like") { + message.likes++; + } else { + message.dislikes++; + } + + const update = { + type: "reaction", + messageId: message.id, + text: req.body.text, + user: req.body.user, + likes: message.likes, + dislikes: message.dislikes, + }; + + while (callbacksForNewMessages.length > 0) { + const callback = callbacksForNewMessages.pop(); + callback([update]); + } + + res.json(message); +}); + app.listen(PORT, () => { console.log(`server is running on http://localhost:${PORT}`); }); diff --git a/chat-app/frontend/app.js b/chat-app/frontend/app.js index 6f86594d..3273e01d 100644 --- a/chat-app/frontend/app.js +++ b/chat-app/frontend/app.js @@ -13,15 +13,100 @@ async function getMessages() { const messages = await response.json(); messages.forEach((message) => { - const messageElement = document.createElement("p"); - messageElement.textContent = message.text; + if (message.type === "reaction") { + updateReaction(message); + return; + } + + const messageElement = document.createElement("div"); + messageElement.id = "message-" + message.id; + messageElement.classList.add("message"); + + // Message text + const textElement = document.createElement("div"); + textElement.classList.add("message-text"); + textElement.textContent = message.text; + + // Reactions area + const reactionsElement = document.createElement("div"); + reactionsElement.classList.add("reactions"); + + // Like button + const likeButton = document.createElement("button"); + likeButton.textContent = "👍"; + + likeButton.addEventListener("click", () => { + reactToMessage(message.id, "like"); + }); + + // Like count + const likeElement = document.createElement("span"); + likeElement.classList.add("like-count"); + likeElement.textContent = message.likes > 0 ? ` ${message.likes}` : ""; + + // Dislike button + const dislikeButton = document.createElement("button"); + dislikeButton.textContent = "👎"; + + dislikeButton.addEventListener("click", () => { + reactToMessage(message.id, "dislike"); + }); + + // Dislike count + const dislikeElement = document.createElement("span"); + dislikeElement.classList.add("dislike-count"); + dislikeElement.textContent = + message.dislikes > 0 ? ` ${message.dislikes}` : ""; + + // Put reactions together + reactionsElement.appendChild(likeButton); + reactionsElement.appendChild(likeElement); + reactionsElement.appendChild(dislikeButton); + reactionsElement.appendChild(dislikeElement); + + // Put text and reactions inside message box + messageElement.appendChild(textElement); + messageElement.appendChild(reactionsElement); messagesContainer.appendChild(messageElement); + lastMessageId = message.id; }); getMessages(); } +// React to message; +async function reactToMessage(messageId, reaction) { + await fetch( + `https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages/${messageId}/reaction`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + reaction: reaction, + }), + }, + ); +} + +// Update Reaction: like / dislike; +function updateReaction(update) { + const messageElement = document.getElementById("message-" + update.messageId); + + if (!messageElement) { + return; + } + + const likeElement = messageElement.querySelector(".like-count"); + const dislikeElement = messageElement.querySelector(".dislike-count"); + + likeElement.textContent = update.likes > 0 ? ` ${update.likes}` : ""; + + dislikeElement.textContent = update.dislikes > 0 ? ` ${update.dislikes}` : ""; +} + // Send message event: sendButton.addEventListener("click", async () => { const text = messageInput.value; @@ -37,7 +122,7 @@ sendButton.addEventListener("click", async () => { headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ text: text }), + body: JSON.stringify({ text: text, user: "Roman" }), }, ); diff --git a/chat-app/frontend/index.html b/chat-app/frontend/index.html index ba7a580b..46f07a26 100644 --- a/chat-app/frontend/index.html +++ b/chat-app/frontend/index.html @@ -1,18 +1,26 @@ + - Simple Chat + Chat App + -

Chat

+
+
+

Chat App

+
-
+
- - +
+ + +
+
diff --git a/chat-app/frontend/style.css b/chat-app/frontend/style.css new file mode 100644 index 00000000..bce0a02e --- /dev/null +++ b/chat-app/frontend/style.css @@ -0,0 +1,99 @@ +* { + box-sizing: border-box; +} + +body { + margin: 0; + font-family: Arial, sans-serif; + background: #f2f2f2; +} + +.chat-app { + width: 500px; + height: 600px; + margin: 40px auto; + background: white; + border: 1px solid #ccc; + border-radius: 8px; + display: flex; + flex-direction: column; + overflow: hidden; +} + +.chat-header { + text-align: center; + padding: 10px; + border-bottom: 1px solid #ccc; +} + +.chat-header h1 { + margin: 0; + font-size: 24px; +} + +.messages { + flex: 1; + padding: 20px; + overflow-y: auto; + display: flex; + flex-direction: column; +} + +.message { + max-width: 70%; + padding: 10px; + margin-bottom: 15px; + border: 1px solid #ccc; + border-radius: 8px; + word-wrap: break-word; +} + +.received { + align-self: flex-start; +} + +.sent { + align-self: flex-end; +} + +.reactions { + margin-top: 8px; +} + +.reactions button { + border: none; + background: none; + cursor: pointer; + padding: 3px 6px; +} + +.reactions button:hover { + background: #f0f0f0; + border-radius: 4px; +} + +.chat-input { + display: flex; + padding: 10px; + border-top: 1px solid #ccc; +} + +#message-input { + flex: 1; + padding: 10px; + border: 1px solid #ccc; + border-radius: 4px; +} + +#send-button { + margin-left: 8px; + padding: 10px 20px; + border: 1px solid #ccc; + border-radius: 4px; + background: white; + cursor: pointer; +} + +#send-button:hover { + background: #f0f0f0; +} From 6f1ff101b8af4f0bbae23f606792a7b67c271f1f Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 1 Sep 2026 18:11:27 +0100 Subject: [PATCH 09/11] Refactor chat and add message alignment with reactions --- chat-app/backend/server.js | 51 ++++++++------ chat-app/frontend/app.js | 139 ++++++++++++++++++++++--------------- 2 files changed, 113 insertions(+), 77 deletions(-) diff --git a/chat-app/backend/server.js b/chat-app/backend/server.js index bd5b15a1..7a05840f 100644 --- a/chat-app/backend/server.js +++ b/chat-app/backend/server.js @@ -1,4 +1,4 @@ -// import necessary modules +// Import necessary modules import express from "express"; import cors from "cors"; @@ -12,12 +12,32 @@ const PORT = process.env.PORT || 3002; const messages = []; const callbacksForNewMessages = []; -// GET all messages +// Send update to clients waiting for new messages +function notifyClients(update) { + while (callbacksForNewMessages.length > 0) { + const callback = callbacksForNewMessages.pop(); + callback([update]); + } +} + +// Validate message +function isValidMessage(body) { + return ( + typeof body.text === "string" && + body.text.trim() !== "" && + typeof body.clientId === "string" && + body.clientId.trim() !== "" + ); +} + +// GET messages app.get("/messages", (req, res) => { const since = Number(req.query.since); console.log("Client asked for messages since:", since); + const newMessages = messages.filter((message) => message.id > since); + console.log("Sending messages:", newMessages); if (newMessages.length === 0) { @@ -29,20 +49,15 @@ app.get("/messages", (req, res) => { // POST a new message app.post("/messages", (req, res) => { - // Validate the request body - if ( - typeof req.body.text !== "string" || - req.body.text.trim() === "" || - typeof req.body.user !== "string" || - req.body.user.trim() === "" - ) { - res.status(400).send("Expected a username and a non-empty text string."); + if (!isValidMessage(req.body)) { + res.status(400).send("Expected a client ID and a non-empty text string."); return; } const message = { id: messages.length, text: req.body.text.trim(), + clientId: req.body.clientId, likes: 0, dislikes: 0, }; @@ -50,14 +65,13 @@ app.post("/messages", (req, res) => { console.log("Received message:", message); messages.push(message); - while (callbacksForNewMessages.length > 0) { - const callback = callbacksForNewMessages.pop(); - callback([message]); - } + + notifyClients(message); res.status(201).json(message); }); +// POST a reaction app.post("/messages/:id/reaction", (req, res) => { const messageId = Number(req.params.id); const reaction = req.body.reaction; @@ -83,20 +97,15 @@ app.post("/messages/:id/reaction", (req, res) => { const update = { type: "reaction", messageId: message.id, - text: req.body.text, - user: req.body.user, likes: message.likes, dislikes: message.dislikes, }; - while (callbacksForNewMessages.length > 0) { - const callback = callbacksForNewMessages.pop(); - callback([update]); - } + notifyClients(update); res.json(message); }); app.listen(PORT, () => { - console.log(`server is running on http://localhost:${PORT}`); + console.log(`Server is running on http://localhost:${PORT}`); }); diff --git a/chat-app/frontend/app.js b/chat-app/frontend/app.js index 3273e01d..ff08a06f 100644 --- a/chat-app/frontend/app.js +++ b/chat-app/frontend/app.js @@ -1,81 +1,105 @@ -// touch html elements: +// Touch HTML elements: const messagesContainer = document.getElementById("messages"); const messageInput = document.getElementById("message-input"); const sendButton = document.getElementById("send-button"); let lastMessageId = -1; +const clientId = crypto.randomUUID(); -// Get message from server; -async function getMessages() { - const response = await fetch( - `https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages?since=${lastMessageId}`, - ); - const messages = await response.json(); +// Create reactions area +function createReactionsElement(message) { + const reactionsElement = document.createElement("div"); + reactionsElement.classList.add("reactions"); - messages.forEach((message) => { - if (message.type === "reaction") { - updateReaction(message); - return; - } + // Like button + const likeButton = document.createElement("button"); + likeButton.textContent = "👍"; - const messageElement = document.createElement("div"); - messageElement.id = "message-" + message.id; - messageElement.classList.add("message"); + likeButton.addEventListener("click", () => { + reactToMessage(message.id, "like"); + }); - // Message text - const textElement = document.createElement("div"); - textElement.classList.add("message-text"); - textElement.textContent = message.text; + // Like count + const likeElement = document.createElement("span"); + likeElement.classList.add("like-count"); + likeElement.textContent = message.likes > 0 ? ` ${message.likes}` : ""; - // Reactions area - const reactionsElement = document.createElement("div"); - reactionsElement.classList.add("reactions"); + // Dislike button + const dislikeButton = document.createElement("button"); + dislikeButton.textContent = "👎"; - // Like button - const likeButton = document.createElement("button"); - likeButton.textContent = "👍"; + dislikeButton.addEventListener("click", () => { + reactToMessage(message.id, "dislike"); + }); - likeButton.addEventListener("click", () => { - reactToMessage(message.id, "like"); - }); + // Dislike count + const dislikeElement = document.createElement("span"); + dislikeElement.classList.add("dislike-count"); + dislikeElement.textContent = + message.dislikes > 0 ? ` ${message.dislikes}` : ""; - // Like count - const likeElement = document.createElement("span"); - likeElement.classList.add("like-count"); - likeElement.textContent = message.likes > 0 ? ` ${message.likes}` : ""; + // Put reactions together + reactionsElement.appendChild(likeButton); + reactionsElement.appendChild(likeElement); + reactionsElement.appendChild(dislikeButton); + reactionsElement.appendChild(dislikeElement); - // Dislike button - const dislikeButton = document.createElement("button"); - dislikeButton.textContent = "👎"; + return reactionsElement; +} - dislikeButton.addEventListener("click", () => { - reactToMessage(message.id, "dislike"); - }); +// Create message element +function createMessageElement(message) { + const messageElement = document.createElement("div"); + messageElement.id = "message-" + message.id; + messageElement.classList.add("message"); + + // Decide whether message was sent or received + if (message.clientId === clientId) { + messageElement.classList.add("sent"); + } else { + messageElement.classList.add("received"); + } - // Dislike count - const dislikeElement = document.createElement("span"); - dislikeElement.classList.add("dislike-count"); - dislikeElement.textContent = - message.dislikes > 0 ? ` ${message.dislikes}` : ""; + // Message text + const textElement = document.createElement("div"); + textElement.classList.add("message-text"); + textElement.textContent = message.text; - // Put reactions together - reactionsElement.appendChild(likeButton); - reactionsElement.appendChild(likeElement); - reactionsElement.appendChild(dislikeButton); - reactionsElement.appendChild(dislikeElement); + // Reactions + const reactionsElement = createReactionsElement(message); - // Put text and reactions inside message box - messageElement.appendChild(textElement); - messageElement.appendChild(reactionsElement); + // Put text and reactions inside message box + messageElement.appendChild(textElement); + messageElement.appendChild(reactionsElement); + + return messageElement; +} + +// Get messages from server +async function getMessages() { + const response = await fetch( + `https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages?since=${lastMessageId}`, + ); + + const messages = await response.json(); + + messages.forEach((message) => { + if (message.type === "reaction") { + updateReaction(message); + return; + } + + const messageElement = createMessageElement(message); messagesContainer.appendChild(messageElement); lastMessageId = message.id; }); + getMessages(); } -// React to message; +// React to message async function reactToMessage(messageId, reaction) { await fetch( `https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages/${messageId}/reaction`, @@ -91,7 +115,7 @@ async function reactToMessage(messageId, reaction) { ); } -// Update Reaction: like / dislike; +// Update reaction: like / dislike function updateReaction(update) { const messageElement = document.getElementById("message-" + update.messageId); @@ -103,11 +127,10 @@ function updateReaction(update) { const dislikeElement = messageElement.querySelector(".dislike-count"); likeElement.textContent = update.likes > 0 ? ` ${update.likes}` : ""; - dislikeElement.textContent = update.dislikes > 0 ? ` ${update.dislikes}` : ""; } -// Send message event: +// Send message event sendButton.addEventListener("click", async () => { const text = messageInput.value; @@ -117,12 +140,16 @@ sendButton.addEventListener("click", async () => { await fetch( "https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages", + { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ text: text, user: "Roman" }), + body: JSON.stringify({ + text: text, + clientId: clientId, + }), }, ); From 64062c8be5ad295e102b6392327045d5360bf37b Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 2 Sep 2026 11:11:23 +0100 Subject: [PATCH 10/11] saved clientId in local storage --- chat-app/frontend/app.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/chat-app/frontend/app.js b/chat-app/frontend/app.js index ff08a06f..9c9ac50a 100644 --- a/chat-app/frontend/app.js +++ b/chat-app/frontend/app.js @@ -4,7 +4,12 @@ const messageInput = document.getElementById("message-input"); const sendButton = document.getElementById("send-button"); let lastMessageId = -1; -const clientId = crypto.randomUUID(); +let clientId = localStorage.getItem("clientId"); + +if (!clientId) { + clientId = crypto.randomUUID(); + localStorage.setItem("clientId", clientId); +} // Create reactions area function createReactionsElement(message) { From b91b76cd8e7edd986549a4059df4205bd3f638b8 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 8 Sep 2026 13:55:43 +0100 Subject: [PATCH 11/11] Refactor API URL into a constant --- chat-app/frontend/app.js | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/chat-app/frontend/app.js b/chat-app/frontend/app.js index 9c9ac50a..ef27c4e4 100644 --- a/chat-app/frontend/app.js +++ b/chat-app/frontend/app.js @@ -5,6 +5,7 @@ const sendButton = document.getElementById("send-button"); let lastMessageId = -1; let clientId = localStorage.getItem("clientId"); +const API_URL = "https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy"; if (!clientId) { clientId = crypto.randomUUID(); @@ -82,9 +83,7 @@ function createMessageElement(message) { // Get messages from server async function getMessages() { - const response = await fetch( - `https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages?since=${lastMessageId}`, - ); + const response = await fetch(`${API_URL}/messages?since=${lastMessageId}`); const messages = await response.json(); @@ -106,18 +105,15 @@ async function getMessages() { // React to message async function reactToMessage(messageId, reaction) { - await fetch( - `https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages/${messageId}/reaction`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - reaction: reaction, - }), + await fetch(`${API_URL}/messages/${messageId}/reaction`, { + method: "POST", + headers: { + "Content-Type": "application/json", }, - ); + body: JSON.stringify({ + reaction: reaction, + }), + }); } // Update reaction: like / dislike @@ -143,9 +139,7 @@ sendButton.addEventListener("click", async () => { return; } - await fetch( - "https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages", - + await fetch(`${API_URL}/messages`, { method: "POST", headers: {