From 2bc2b272df36cea9aa5c23aa66e6e3185144a492 Mon Sep 17 00:00:00 2001 From: Yan Kazymyr Date: Wed, 8 Apr 2026 22:07:16 +0300 Subject: [PATCH 1/2] Solution --- src/createServer.js | 103 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..3d456f3 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,107 @@ 'use strict'; +const http = require('http'); +const fs = require('fs'); + +const form = ` +
+ + + + +
+`; + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((req, res) => { + if (req.method === 'GET') { + if (req.url === '/') { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(form); + } else if (req.url === '/add-expense') { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Incorrect request'); + } else { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Page not found'); + } + + return; + } + + if (req.method === 'POST') { + if (req.url === '/add-expense') { + let body = ''; + + req.on('data', (chunk) => (body += chunk)); + + req.on('end', () => { + let expenseData; + + if (req.headers['content-type'] === 'application/json') { + expenseData = JSON.parse(body); + } else { + const params = new URLSearchParams(body); + + expenseData = Object.fromEntries(params); + } + + const dateObj = new Date(expenseData.date); + const isValidDate = dateObj instanceof Date && !isNaN(dateObj); + + const isValidTitle = + expenseData.title && expenseData.title.trim().length > 0; + + const amount = parseFloat(expenseData.amount); + const isValidAmount = !isNaN(amount) && amount > 0; + + if (!isValidDate || !isValidTitle || !isValidAmount) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Invalid data'); + } else { + const filePath = 'db/expense.json'; + + if (!fs.existsSync('db')) { + fs.mkdirSync('db'); + } + + if (!fs.existsSync(filePath)) { + fs.writeFileSync(filePath, JSON.stringify({})); + } + + // const raw = fs.readFileSync(filePath, 'utf8'); + // const arr = JSON.parse(raw); + const record = { + date: expenseData.date, + title: expenseData.title, + amount: expenseData.amount, + }; + + // arr.push(record); + + fs.writeFileSync(filePath, JSON.stringify(record, null, 2)); + + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(expenseData)); + } + }); + } + } + }); } module.exports = { From ffe77f32599909341bf76b9df279cddb2b1ff252 Mon Sep 17 00:00:00 2001 From: Yan Kazymyr Date: Wed, 8 Apr 2026 22:24:16 +0300 Subject: [PATCH 2/2] Fix --- src/createServer.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 3d456f3..d4afec7 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -80,23 +80,29 @@ function createServer() { } if (!fs.existsSync(filePath)) { - fs.writeFileSync(filePath, JSON.stringify({})); + fs.writeFileSync(filePath, JSON.stringify([])); } - // const raw = fs.readFileSync(filePath, 'utf8'); - // const arr = JSON.parse(raw); const record = { date: expenseData.date, title: expenseData.title, amount: expenseData.amount, }; - // arr.push(record); - fs.writeFileSync(filePath, JSON.stringify(record, null, 2)); - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify(expenseData)); + if (req.headers['content-type'] === 'application/json') { + // JSON запит — повертай JSON + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(record)); + } else { + // HTML форма — повертай HTML + res.writeHead(200, { 'Content-Type': 'text/html' }); + + const html = `
${JSON.stringify(record, null, 2)}
`; + + res.end(html); + } } }); }