From 604d5b09cb2f89d651c42a97dc60d62de52ca128 Mon Sep 17 00:00:00 2001 From: Bohdan Petliuk Date: Fri, 8 May 2026 02:57:13 +0300 Subject: [PATCH 1/2] sol --- .github/workflows/test.yml-template | 23 ++++++++++ package-lock.json | 9 ++-- package.json | 2 +- src/createServer.js | 66 ++++++++++++++++++++++++++++- src/index.html | 21 +++++++++ 5 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/test.yml-template 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..4fbbc4f 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,10 +1468,11 @@ } }, "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, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", 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..8d98687 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,70 @@ 'use strict'; +const http = require('http'); + +const fs = require('fs'); + +const querystring = require('querystring'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((req, res) => { + if (req.url === '/' && req.method === 'GET') { + fs.readFile('./src/index.html', (err, date) => { + if (err) { + res.statusCode = 500; + res.end(); + + return; + } + res.setHeader('Content-Type', 'text/html'); + res.statusCode = 200; + res.end(date); + }); + } else if (req.url === '/add-expense' && req.method === 'POST') { + // обробити дані + let body = ''; + + req.on('data', (chunk) => { + body += chunk; + }); + + req.on('end', () => { + const contentType = req.headers['content-type']; + let parsed; + + if (contentType.includes('application/json')) { + parsed = JSON.parse(body); + } else { + parsed = querystring.parse(body); + } + + if (!parsed.date || !parsed.title || !parsed.amount) { + res.statusCode = 400; + res.end('Bad request'); + + return; + } + + const data = JSON.stringify(parsed, null, 2); + + fs.writeFile('./db/expense.json', data, (error) => { + if (error) { + res.statusCode = 500; + res.end(); + + return; + } + + res.setHeader('Content-Type', 'text/html'); + res.statusCode = 200; + res.end(`
${data}
`); + }); + }); + } else { + res.statusCode = 404; + res.end(); + } + }); } module.exports = { diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..e5708b7 --- /dev/null +++ b/src/index.html @@ -0,0 +1,21 @@ + + + + + Compression App + + +
+
+

Date

+ +

Title

+ +

Amount

+ +
+ + +
+ + From c58de69a48f8c810afa010e6a65ea8f39880b4f9 Mon Sep 17 00:00:00 2001 From: Bohdan Petliuk Date: Fri, 8 May 2026 03:01:40 +0300 Subject: [PATCH 2/2] sol2 --- src/index.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.html b/src/index.html index e5708b7..36c525b 100644 --- a/src/index.html +++ b/src/index.html @@ -5,13 +5,13 @@ Compression App -
+
-

Date

+ -

Title

+ -

Amount

+