From 8a685c5d66207461a4bbd51f4e18a6350c581825 Mon Sep 17 00:00:00 2001 From: Nazarii-Lesniak Date: Sun, 22 Mar 2026 13:47:05 +0200 Subject: [PATCH 1/2] solution --- src/createServer.js | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..649ed31 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,47 @@ 'use strict'; +const http = require('http'); +const fs = require('fs'); +const path = require('path'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((request, response) => { + if (request.url === '/add-expense' && request.method === 'POST') { + const chunks = []; + + request.on('data', (chunk) => { + chunks.push(chunk); + }); + + request.on('end', () => { + try { + const parsedData = JSON.parse(Buffer.concat(chunks).toString()); + + if (!parsedData.amount || !parsedData.title || !parsedData.date) { + response.statusCode = 400; + response.end('Invalid data'); + + return; + } + + const db = path.join(__dirname, '../db/expense.json'); + const stringifiedData = JSON.stringify(parsedData); + + fs.writeFileSync(db, stringifiedData); + + response.statusCode = 200; + response.setHeader('Content-Type', 'application/json'); + response.end(JSON.stringify(parsedData)); + } catch { + response.statusCode = 400; + response.end('Invalid data'); + } + }); + } else { + response.statusCode = 404; + response.end('Not Found'); + } + }); } module.exports = { From cd2cbd7b731712e91989299ad6449135b0021790 Mon Sep 17 00:00:00 2001 From: Nazarii-Lesniak Date: Sun, 22 Mar 2026 16:40:21 +0200 Subject: [PATCH 2/2] changed --- src/createServer.js | 61 ++++++++++++++++++++++++++++++++++++++------- src/index.html | 16 ++++++++++++ 2 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 src/index.html diff --git a/src/createServer.js b/src/createServer.js index 649ed31..d7d3192 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -6,7 +6,22 @@ const path = require('path'); function createServer() { return http.createServer((request, response) => { - if (request.url === '/add-expense' && request.method === 'POST') { + if (request.url === '/' && request.method === 'GET') { + const htmlPath = path.resolve(__dirname, 'index.html'); + + fs.readFile(htmlPath, 'utf-8', (error, data) => { + if (error) { + response.statusCode = 500; + response.end('Internal Server Error'); + + return; + } + + response.statusCode = 200; + response.setHeader('Content-Type', 'text/html'); + response.end(data); + }); + } else if (request.url === '/add-expense' && request.method === 'POST') { const chunks = []; request.on('data', (chunk) => { @@ -15,7 +30,18 @@ function createServer() { request.on('end', () => { try { - const parsedData = JSON.parse(Buffer.concat(chunks).toString()); + const bodyStr = Buffer.concat(chunks).toString(); + let parsedData; + + const contentType = request.headers['content-type'] || ''; + + if (contentType === 'application/json') { + parsedData = JSON.parse(bodyStr); + } else { + const params = new URLSearchParams(bodyStr); + + parsedData = Object.fromEntries(params); + } if (!parsedData.amount || !parsedData.title || !parsedData.date) { response.statusCode = 400; @@ -24,15 +50,32 @@ function createServer() { return; } - const db = path.join(__dirname, '../db/expense.json'); - const stringifiedData = JSON.stringify(parsedData); + const dbPath = path.join(__dirname, '../db/expense.json'); + + fs.writeFile(dbPath, JSON.stringify(parsedData, null, 2), (error) => { + if (error) { + response.statusCode = 500; + response.end('Internal Server Error'); + + return; + } + + const accept = request.headers.accept || ''; - fs.writeFileSync(db, stringifiedData); + if (accept.includes('text/html')) { + response.statusCode = 200; + response.setHeader('Content-Type', 'text/html'); - response.statusCode = 200; - response.setHeader('Content-Type', 'application/json'); - response.end(JSON.stringify(parsedData)); - } catch { + response.end(` +
${JSON.stringify(parsedData, null, 2)}
+ `); + } else { + response.statusCode = 200; + response.setHeader('Content-Type', 'application/json'); + response.end(JSON.stringify(parsedData)); + } + }); + } catch (error) { response.statusCode = 400; response.end('Invalid data'); } diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..1f6605c --- /dev/null +++ b/src/index.html @@ -0,0 +1,16 @@ + + + + + + Document + + +
+ + + + +
+ +