From 7ee69ca1433ba34503acf06acfadadba49844135 Mon Sep 17 00:00:00 2001 From: Alina Luchytska Date: Mon, 23 Mar 2026 19:55:16 +0200 Subject: [PATCH 1/2] add solution --- src/createServer.js | 106 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..7d2b082 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,114 @@ 'use strict'; +const http = require('http'); +const fs = require('fs'); +const path = require('path'); +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') { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/html'); + + res.end(` + + + Expense Form + + +

Add Expense

+
+ + + + + + + + + + +
+ + + `); + } else if ( + (req.url === '/submit-expense' || req.url === '/add-expense') && + req.method === 'POST' + ) { + const chunks = []; + + req.on('data', (chunk) => { + chunks.push(chunk); + }); + + req.on('end', () => { + try { + const body = Buffer.concat(chunks).toString(); + + if (!body) { + res.statusCode = 400; + res.end('Empty body'); + + return; + } + + const contentType = req.headers['content-type'] || ''; + let bodyParsed; + + if (contentType.includes('application/json')) { + bodyParsed = JSON.parse(body); + } else { + bodyParsed = querystring.parse(body); + } + + const { date, title, amount } = bodyParsed; + + if ( + !date || + !title || + !amount || + date.trim() === '' || + title.trim() === '' || + amount.toString().trim() === '' + ) { + res.statusCode = 400; + res.setHeader('Content-Type', 'text/plain'); + + res.end( + 'Missing required fields: date, title and amount are required', + ); + + return; + } + + const dbDir = path.join(__dirname, '..', 'db'); + + if (!fs.existsSync(dbDir)) { + fs.mkdirSync(dbDir); + } + + const filePath = path.join(dbDir, 'expense.json'); + + fs.writeFileSync(filePath, JSON.stringify(bodyParsed, null, 2)); + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + + res.end(JSON.stringify(bodyParsed)); + } catch (err) { + res.statusCode = 500; + res.end('Server error: ' + err.message); + } + }); + } else { + res.statusCode = 404; + res.setHeader('Content-Type', 'text/plain'); + res.end(`Invalid url: ${req.url}`); + } + }); } module.exports = { From 091d96c3573181edaf02089d3650b60f4e93bea8 Mon Sep 17 00:00:00 2001 From: Alina Luchytska Date: Tue, 24 Mar 2026 12:30:41 +0200 Subject: [PATCH 2/2] fix solution --- src/createServer.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 7d2b082..ef5c145 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -95,9 +95,21 @@ function createServer() { fs.writeFileSync(filePath, JSON.stringify(bodyParsed, null, 2)); res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(bodyParsed)); + if (req.url === '/add-expense') { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(bodyParsed)); + } else { + res.setHeader('Content-Type', 'text/html'); + + res.end(` + + +
${JSON.stringify(bodyParsed, null, 2)}
+ + + `); + } } catch (err) { res.statusCode = 500; res.end('Server error: ' + err.message);