From 999265c2951397fb228aec5ac8abddcf957ad212 Mon Sep 17 00:00:00 2001
From: RomanHasiuk
Date: Wed, 11 Feb 2026 20:01:29 +0200
Subject: [PATCH 1/3] Solution
---
db/expense.json | 6 +-
public/index.html | 155 ++++++++++++++++++++++++++++++++++++++++++++
src/createServer.js | 62 +++++++++++++++++-
3 files changed, 216 insertions(+), 7 deletions(-)
create mode 100644 public/index.html
diff --git a/db/expense.json b/db/expense.json
index 1bc75a6..1257e7a 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
+{"date":"2024-01-25","title":"Test Expense","amount":"100"}
\ No newline at end of file
diff --git a/public/index.html b/public/index.html
new file mode 100644
index 0000000..419bd39
--- /dev/null
+++ b/public/index.html
@@ -0,0 +1,155 @@
+
+
+
+
+
+
+ Document
+
+
+
+
+
+
💰Expense Tracker
+
Додайте нову витрату
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/createServer.js b/src/createServer.js
index 1cf1dda..f2cd1e6 100644
--- a/src/createServer.js
+++ b/src/createServer.js
@@ -1,8 +1,66 @@
'use strict';
+const http = require('http');
+const path = require('path');
+const fs = require('fs');
+const querystring = require('querystring');
+
+const dataPath = path.resolve(__dirname, '..', 'db', 'expense.json');
+const htmlPath = path.resolve(__dirname, '..', 'public', 'index.html');
+
function createServer() {
- /* Write your code here */
- // Return instance of http.Server class
+ return http.createServer((req, res) => {
+ if (req.method === 'GET' && req.url === '/') {
+ fs.readFile(htmlPath, (err, data) => {
+ if (err) {
+ res.writeHead(500);
+
+ return res.end('Error loading index.html');
+ }
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
+ res.end(data);
+ });
+
+ return;
+ }
+
+ if (req.method === 'POST' && req.url === '/add-expense') {
+ let body = '';
+
+ req.on('data', (chunk) => {
+ body += chunk.toString();
+ });
+
+ req.on('end', () => {
+ try {
+ const contentType = req.headers['content-type'];
+ const expense =
+ contentType === 'application/json'
+ ? JSON.parse(body)
+ : querystring.parse(body);
+
+ if (!expense.date || !expense.title || !expense.amount) {
+ res.writeHead(400, { 'Content-Type': 'text/plain' });
+
+ return res.end('Missing fields');
+ }
+
+ fs.writeFileSync(dataPath, JSON.stringify(expense));
+
+ res.writeHead(200, { 'Content-Type': 'application/json' });
+ res.end(JSON.stringify(expense));
+ } catch (error) {
+ res.writeHead(400);
+ res.end('Invalid Data');
+ }
+ });
+
+ return;
+ }
+
+ res.writeHead(404);
+ res.end('Not Found');
+ });
}
module.exports = {
From 3b20281573f10ff006ea9c33cb99c97aaa34ace0 Mon Sep 17 00:00:00 2001
From: RomanHasiuk
Date: Wed, 11 Feb 2026 21:01:04 +0200
Subject: [PATCH 2/3] Solution #2
---
db/expense.json | 6 +++++-
src/createServer.js | 33 ++++++++++++++++++++++++++-------
2 files changed, 31 insertions(+), 8 deletions(-)
diff --git a/db/expense.json b/db/expense.json
index 1257e7a..788f275 100644
--- a/db/expense.json
+++ b/db/expense.json
@@ -1 +1,5 @@
-{"date":"2024-01-25","title":"Test Expense","amount":"100"}
\ No newline at end of file
+{
+ "date": "2026-02-05",
+ "title": "oih8jhy",
+ "amount": "987"
+}
\ No newline at end of file
diff --git a/src/createServer.js b/src/createServer.js
index f2cd1e6..d59bf4e 100644
--- a/src/createServer.js
+++ b/src/createServer.js
@@ -34,10 +34,10 @@ function createServer() {
req.on('end', () => {
try {
const contentType = req.headers['content-type'];
- const expense =
- contentType === 'application/json'
- ? JSON.parse(body)
- : querystring.parse(body);
+ const isJson =
+ contentType && contentType.startsWith('application/json');
+
+ const expense = isJson ? JSON.parse(body) : querystring.parse(body);
if (!expense.date || !expense.title || !expense.amount) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
@@ -45,10 +45,29 @@ function createServer() {
return res.end('Missing fields');
}
- fs.writeFileSync(dataPath, JSON.stringify(expense));
+ const prettyJson = JSON.stringify(expense, null, 2);
+
+ fs.writeFileSync(dataPath, prettyJson);
- res.writeHead(200, { 'Content-Type': 'application/json' });
- res.end(JSON.stringify(expense));
+ if (isJson) {
+ res.writeHead(200, { 'Content-Type': 'application/json' });
+ res.end(JSON.stringify(expense));
+ } else {
+ const responseHtml = `
+
+
+ Expense Saved
+
+ Expense Added Successfully
+ ${prettyJson}
+ Add another one
+
+
+ `;
+
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
+ res.end(responseHtml);
+ }
} catch (error) {
res.writeHead(400);
res.end('Invalid Data');
From 6d4f116959cee3e9c21f0cb83e3b6861583c98b4 Mon Sep 17 00:00:00 2001
From: RomanHasiuk
Date: Wed, 11 Feb 2026 21:57:51 +0200
Subject: [PATCH 3/3] Solution #3
---
db/expense.json | 6 +++---
public/index.html | 2 +-
src/createServer.js | 26 +++++++++++++-------------
3 files changed, 17 insertions(+), 17 deletions(-)
diff --git a/db/expense.json b/db/expense.json
index 788f275..1bc75a6 100644
--- a/db/expense.json
+++ b/db/expense.json
@@ -1,5 +1,5 @@
{
- "date": "2026-02-05",
- "title": "oih8jhy",
- "amount": "987"
+ "date": "2024-01-25",
+ "title": "Test Expense",
+ "amount": "100"
}
\ No newline at end of file
diff --git a/public/index.html b/public/index.html
index 419bd39..4a5cdc9 100644
--- a/public/index.html
+++ b/public/index.html
@@ -147,7 +147,7 @@ 💰Expense Tracker
-
+
- Expense Added Successfully
- ${prettyJson}
- Add another one
-
diff --git a/src/createServer.js b/src/createServer.js
index d59bf4e..6d88c03 100644
--- a/src/createServer.js
+++ b/src/createServer.js
@@ -34,10 +34,12 @@ function createServer() {
req.on('end', () => {
try {
const contentType = req.headers['content-type'];
- const isJson =
+ const isJsonRequest =
contentType && contentType.startsWith('application/json');
- const expense = isJson ? JSON.parse(body) : querystring.parse(body);
+ const expense = isJsonRequest
+ ? JSON.parse(body)
+ : querystring.parse(body);
if (!expense.date || !expense.title || !expense.amount) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
@@ -49,21 +51,19 @@ function createServer() {
fs.writeFileSync(dataPath, prettyJson);
- if (isJson) {
+ if (isJsonRequest) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(expense));
} else {
const responseHtml = `
-
-
-