From cd68572339d2a50fb05ccea66e1515f76c1d5f40 Mon Sep 17 00:00:00 2001 From: Olga Berezhna <57196165+oberezhnay@users.noreply.github.com> Date: Wed, 7 Jan 2026 22:04:33 +0200 Subject: [PATCH 1/4] Solution --- .github/workflows/test.yml-template | 23 +++++++++++ package-lock.json | 8 ++-- package.json | 2 +- src/createServer.js | 59 ++++++++++++++++++++++++++++- src/db/expense.json | 5 +++ src/index.html | 17 +++++++++ 6 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/test.yml-template create mode 100644 src/db/expense.json create mode 100644 src/index.html diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 0000000..bb13dfc --- /dev/null +++ b/.github/workflows/test.yml-template @@ -0,0 +1,23 @@ +name: Test + +on: + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [20.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/package-lock.json b/package-lock.json index 28a4d31..270ae4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "axios": "^1.7.2", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", @@ -1468,9 +1468,9 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, "dependencies": { "@octokit/rest": "^17.11.2", diff --git a/package.json b/package.json index 8a92721..cd53c9e 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "axios": "^1.7.2", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..7f1d95a 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,63 @@ 'use strict'; +const http = require('http'); +const fs = require('fs'); +const path = require('path'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer(async (req, res) => { + const { url, method } = req; + + if (method === 'GET' && url === '/') { + const htmlPath = path.join(__dirname, 'index.html'); + + res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }); + fs.createReadStream(htmlPath).pipe(res); + + return; + } + + if ( + method === 'POST' && + (url === '/add-expense' || url === '/submit-expense') + ) { + let body = ''; + + req.on('data', (chunk) => { + body += chunk; + }); + + req.on('end', () => { + let expense; + + try { + expense = JSON.parse(body); + } catch { + res.writeHead(400); + + return res.end('Invalid JSON'); + } + + if (!expense.date || !expense.title || expense.amount === undefined) { + res.writeHead(400); + + return res.end('Invalid form data'); + } + + const dbPath = path.resolve('db', 'expense.json'); + + fs.writeFileSync(dbPath, JSON.stringify(expense, null, 2), 'utf-8'); + + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(expense)); + }); + + return; + } + + res.writeHead(404); + res.end('Not Found'); + }); } module.exports = { diff --git a/src/db/expense.json b/src/db/expense.json new file mode 100644 index 0000000..1bc75a6 --- /dev/null +++ b/src/db/expense.json @@ -0,0 +1,5 @@ +{ + "date": "2024-01-25", + "title": "Test Expense", + "amount": "100" +} \ No newline at end of file diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..1670d32 --- /dev/null +++ b/src/index.html @@ -0,0 +1,17 @@ + + + + + + Document + + +
+ + + + + +
+ + From ac98c99f495c172cc4c66c39fde4135ef12ce946 Mon Sep 17 00:00:00 2001 From: Olga Berezhna <57196165+oberezhnay@users.noreply.github.com> Date: Wed, 7 Jan 2026 22:39:33 +0200 Subject: [PATCH 2/4] Fixes after review --- src/createServer.js | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 7f1d95a..3f52084 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -28,10 +28,20 @@ function createServer() { }); req.on('end', () => { + const contentType = req.headers['content-type'] || ''; + const isJsonRequest = contentType.includes('application/json'); let expense; try { - expense = JSON.parse(body); + if (contentType.includes('application/json')) { + expense = JSON.parse(body); + } else if ( + contentType.includes('application/x-www-form-urlencoded') + ) { + expense = Object.fromEntries(new URLSearchParams(body)); + } else { + expense = {}; + } } catch { res.writeHead(400); @@ -46,10 +56,34 @@ function createServer() { const dbPath = path.resolve('db', 'expense.json'); + // NOTE: + // We intentionally overwrite expense.json on each request + // because task tests expect the file to contain a single expense object. + // In a real application we would store an array of expenses. fs.writeFileSync(dbPath, JSON.stringify(expense, null, 2), 'utf-8'); - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify(expense)); + if (isJsonRequest) { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(expense)); + } else { + const prettyJson = JSON.stringify(expense, null, 2); + + res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }); + + res.end(` + + + + + Document + + +

Saved expense

+
${prettyJson}
+ + + `); + } }); return; From 1701b1673746ad2f0cc5dbbf071241f4d9c58bb6 Mon Sep 17 00:00:00 2001 From: Olga Berezhna <57196165+oberezhnay@users.noreply.github.com> Date: Wed, 7 Jan 2026 22:43:14 +0200 Subject: [PATCH 3/4] add fixes --- src/createServer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 3f52084..a8e97b6 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -57,8 +57,9 @@ function createServer() { const dbPath = path.resolve('db', 'expense.json'); // NOTE: - // We intentionally overwrite expense.json on each request - // because task tests expect the file to contain a single expense object. + // expense.json overwrites on each request + // because task tests expect the file to contain + // a single expense object. // In a real application we would store an array of expenses. fs.writeFileSync(dbPath, JSON.stringify(expense, null, 2), 'utf-8'); From a50e84944e43fcbfb3809d79848396dc7ab2a850 Mon Sep 17 00:00:00 2001 From: Olga Berezhna <57196165+oberezhnay@users.noreply.github.com> Date: Wed, 7 Jan 2026 22:49:28 +0200 Subject: [PATCH 4/4] fix structure --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 076e0b2..fa506ba 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,5 @@ # Form data (with Node.js) + Implement an app that - shows an HTML form with an info about an expense (date, title and amount) - receives its data in a POST request