From 55bad04d666c8ed05a7ecced5014edbee2ba0ab7 Mon Sep 17 00:00:00 2001 From: Voloshynd Date: Sun, 20 Jul 2025 16:25:25 +0200 Subject: [PATCH 1/6] add solution of task node_form-data --- src/createServer.js | 124 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 2 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..8810db5 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,128 @@ 'use strict'; +const http = require('node:http'); +const fs = require('node:fs'); +const path = require('node:path'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + const server = new http.Server(); + const filePath = path.join(__dirname, '..', 'db', 'expense.json'); + + server.on('request', (req, res) => { + if (req.method === 'POST' && req.url === '/submit-expense') { + if (!fs.existsSync(path.dirname(filePath))) { + fs.mkdirSync(path.dirname(filePath), { recursive: true }); + } + + if (!fs.existsSync(filePath)) { + fs.writeFileSync( + filePath, + JSON.stringify({ expenses: [] }, null, 2), + 'utf-8', + ); + } + + let fields = ''; + + req.on('data', (chunk) => { + fields += chunk.toString(); + }); + + req.on('end', () => { + const params = new URLSearchParams(fields); + const date = params.get('date'); + const title = params.get('title'); + const amount = params.get('amount'); + + if (!date || !title || !amount) { + res.statusCode = 400; + res.end('Missing fields'); + + return null; + } + + const newExpense = { + date, + title, + amount: parseFloat(amount), + }; + + const jsonString = JSON.stringify(newExpense, null, 2); + + const writeStream = fs.createWriteStream(filePath); + + writeStream.on('error', (err) => { + // eslint-disable-next-line no-console + console.error('Write error:', err.message); + }); + + writeStream.on('finish', () => { + // eslint-disable-next-line no-console + console.log('Expense data written successfully.'); + }); + + writeStream.write(jsonString); + writeStream.end(); + + let buffer = ''; + + const readStream = fs.createReadStream(filePath, { encoding: 'utf-8' }); + + readStream.on('data', (chunk) => { + buffer += chunk; + }); + + readStream.on('end', () => { + try { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/html'); + + const data = JSON.parse(buffer); + const formattedJSON = JSON.stringify(data); + + res.end(formattedJSON); + } catch (err) { + // eslint-disable-next-line no-console + console.error('JSON parse error:', err.message); + } + }); + + readStream.on('error', (err) => { + // eslint-disable-next-line no-console + console.error('Read error:', err.message); + }); + }); + } else if (req.url === '/') { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/html'); + + res.end(` +

Add Expense

+
+
+

+ +
+

+ +
+

+ + +
+ `); + } else { + res.statusCode = 404; + res.end('Not found'); + } + }); + + server.on('error', (err) => { + // eslint-disable-next-line no-console + console.error('Server error:', err); + }); + + return server; } module.exports = { From 615a100bb60edb3643f408d6bf0960436b92e46d Mon Sep 17 00:00:00 2001 From: Voloshynd Date: Sun, 20 Jul 2025 19:19:55 +0200 Subject: [PATCH 2/6] critical issue fixed --- src/createServer.js | 76 +++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 8810db5..7a5af0f 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -47,49 +47,52 @@ function createServer() { amount: parseFloat(amount), }; - const jsonString = JSON.stringify(newExpense, null, 2); + fs.readFile(filePath, 'utf-8', (err, data) => { + if (err) { + res.statusCode = 500; + res.end('Error reading file'); - const writeStream = fs.createWriteStream(filePath); - - writeStream.on('error', (err) => { - // eslint-disable-next-line no-console - console.error('Write error:', err.message); - }); - - writeStream.on('finish', () => { - // eslint-disable-next-line no-console - console.log('Expense data written successfully.'); - }); - - writeStream.write(jsonString); - writeStream.end(); - - let buffer = ''; - - const readStream = fs.createReadStream(filePath, { encoding: 'utf-8' }); + return null; + } - readStream.on('data', (chunk) => { - buffer += chunk; - }); + let expenseData; - readStream.on('end', () => { try { - res.statusCode = 200; - res.setHeader('Content-Type', 'text/html'); + expenseData = JSON.parse(data); - const data = JSON.parse(buffer); - const formattedJSON = JSON.stringify(data); - - res.end(formattedJSON); - } catch (err) { - // eslint-disable-next-line no-console - console.error('JSON parse error:', err.message); + if (!Array.isArray(expenseData.expenses)) { + expenseData.expenses = []; + } + } catch { + expenseData = { expenses: [] }; } - }); - readStream.on('error', (err) => { - // eslint-disable-next-line no-console - console.error('Read error:', err.message); + expenseData.expenses.push(newExpense); + + fs.writeFile( + filePath, + JSON.stringify(expenseData, null, 2), + 'utf-8', + (error) => { + if (error) { + res.statusCode = 500; + res.end('Failed to write file'); + + return null; + } + + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + + res.end( + JSON.stringify( + expenseData.expenses[expenseData.expenses.length - 1], + null, + 2, + ), + ); + }, + ); }); }); } else if (req.url === '/') { @@ -118,7 +121,6 @@ function createServer() { }); server.on('error', (err) => { - // eslint-disable-next-line no-console console.error('Server error:', err); }); From f74aae414619e375d521554fb87c873ef29b12a5 Mon Sep 17 00:00:00 2001 From: Voloshynd Date: Sun, 20 Jul 2025 19:21:52 +0200 Subject: [PATCH 3/6] critical issue with overwriting the entire db/expense.json file has been fixed --- src/createServer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/createServer.js b/src/createServer.js index 7a5af0f..c079b6c 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -121,6 +121,7 @@ function createServer() { }); server.on('error', (err) => { + // eslint-disable-next-line no-console console.error('Server error:', err); }); From 3d928c5322b7fd2934058d7114a4a37966c618ae Mon Sep 17 00:00:00 2001 From: Voloshynd Date: Sun, 20 Jul 2025 19:23:03 +0200 Subject: [PATCH 4/6] critical issue with overwriting the entire db/expense.json file has been fixed --- db/expense.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/db/expense.json b/db/expense.json index 1bc75a6..4cdbcd8 100644 --- a/db/expense.json +++ b/db/expense.json @@ -1,5 +1 @@ -{ - "date": "2024-01-25", - "title": "Test Expense", - "amount": "100" -} \ No newline at end of file +{}git From 2191ae38cfd785edf128fbac6e1008abe72d1a0d Mon Sep 17 00:00:00 2001 From: Voloshynd Date: Sun, 20 Jul 2025 19:25:39 +0200 Subject: [PATCH 5/6] critical issue with overwriting the entire db/expense.json file has been fixed --- db/expense.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/expense.json b/db/expense.json index 4cdbcd8..0967ef4 100644 --- a/db/expense.json +++ b/db/expense.json @@ -1 +1 @@ -{}git +{} From 6017fa29fba1fce0dbe52b030f99da5cb069125b Mon Sep 17 00:00:00 2001 From: Voloshynd Date: Sun, 20 Jul 2025 19:26:40 +0200 Subject: [PATCH 6/6] critical issue with overwriting the entire db/expense.json file has been fixed --- db/expense.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/expense.json b/db/expense.json index 0967ef4..9e26dfe 100644 --- a/db/expense.json +++ b/db/expense.json @@ -1 +1 @@ -{} +{} \ No newline at end of file