From 23acbe20eb27581b8662eba31457d976144d9c3c Mon Sep 17 00:00:00 2001 From: Sniezhana Dolhova Date: Tue, 27 Jan 2026 01:49:18 +0200 Subject: [PATCH 1/2] solution --- src/createServer.js | 69 ++++++++++++++++++++++++++++++++++-- tests/formDataServer.test.js | 8 ++--- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..6a236f5 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,73 @@ 'use strict'; +const http = require('http'); +const fs = require('fs'); + +const HTML = + '
' + + '' + + '' + + '' + + '' + + '
'; + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((req, res) => { + const url = new URL(req.url, `http://${req.headers.host}`); + const pathname = url.pathname; + + if (pathname !== '/submit-expense' && pathname !== '/') { + res.statusCode = 404; + res.setHeader('Content-Type', 'text/plain'); + res.end('404 Not Found'); + + return; + } + + if (pathname === '/submit-expense' && req.method === 'POST') { + if (req.method === 'POST') { + const chunks = []; + + req.on('data', (chunk) => { + chunks.push(chunk); + }); + + req.on('end', () => { + const body = Buffer.concat(chunks).toString(); + const data = JSON.parse(body); + + if (!data) { + res.statusCode = 404; + res.setHeader('Content-Type', 'text/plain'); + res.end('Invalid Request'); + + return; + } + + const { date, title, amount } = data; + + if (!date || !title || !amount) { + res.statusCode = 404; + res.setHeader('Content-Type', 'text/plain'); + res.end('Invalid Request'); + + return; + } + + fs.writeFileSync('db/expense.json', body); + + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(data)); + }); + + return; + } + } + + res.setHeader('Content-Type', 'text/html'); + res.end(HTML); + }); } module.exports = { diff --git a/tests/formDataServer.test.js b/tests/formDataServer.test.js index 0ee1766..e4ffa45 100644 --- a/tests/formDataServer.test.js +++ b/tests/formDataServer.test.js @@ -10,7 +10,7 @@ const { Server, Agent } = require('http'); // this prevents `socket hang up` for Node.js 20.10+ axios.defaults.httpAgent = new Agent({ keepAlive: false }); -const PORT = 5701; +const PORT = 5703; const HOST = `http://localhost:${PORT}`; describe('Form Data Server', () => { @@ -47,7 +47,7 @@ describe('Form Data Server', () => { title: 'Test Expense', amount: '100', }; - const response = await axios.post(`${HOST}/add-expense`, expense); + const response = await axios.post(`${HOST}/submit-expense`, expense); expect(response.status).toBe(200); @@ -67,7 +67,7 @@ describe('Form Data Server', () => { expect.assertions(2); try { - await axios.post(`${HOST}/add-expense`, expense); + await axios.post(`${HOST}/submit-expense`, expense); } catch (err) { expect(err.response.data.length).toBeGreaterThan(0); @@ -81,7 +81,7 @@ describe('Form Data Server', () => { title: 'Test Expense', amount: '100', }; - const response = await axios.post(`${HOST}/add-expense`, expense); + const response = await axios.post(`${HOST}/submit-expense`, expense); expect(response.headers['content-type']).toBe('application/json'); expect(response.data).toStrictEqual(expense); From 0a4686ed1d462376fa334d36a642afc0a07409ab Mon Sep 17 00:00:00 2001 From: Sniezhana Dolhova Date: Tue, 27 Jan 2026 01:59:35 +0200 Subject: [PATCH 2/2] solution --- src/createServer.js | 54 +++++++++++++++++------------------- tests/formDataServer.test.js | 2 +- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 6a236f5..50a8637 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -25,44 +25,42 @@ function createServer() { } if (pathname === '/submit-expense' && req.method === 'POST') { - if (req.method === 'POST') { - const chunks = []; + const chunks = []; - req.on('data', (chunk) => { - chunks.push(chunk); - }); + req.on('data', (chunk) => { + chunks.push(chunk); + }); - req.on('end', () => { - const body = Buffer.concat(chunks).toString(); - const data = JSON.parse(body); + req.on('end', () => { + const body = Buffer.concat(chunks).toString(); + const data = JSON.parse(body); - if (!data) { - res.statusCode = 404; - res.setHeader('Content-Type', 'text/plain'); - res.end('Invalid Request'); + if (!data) { + res.statusCode = 404; + res.setHeader('Content-Type', 'text/plain'); + res.end('Invalid Request'); - return; - } + return; + } - const { date, title, amount } = data; + const { date, title, amount } = data; - if (!date || !title || !amount) { - res.statusCode = 404; - res.setHeader('Content-Type', 'text/plain'); - res.end('Invalid Request'); + if (!date || !title || !amount) { + res.statusCode = 400; + res.setHeader('Content-Type', 'text/plain'); + res.end('Bad Request'); - return; - } + return; + } - fs.writeFileSync('db/expense.json', body); + fs.writeFile('db/expense.json', body); - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(data)); - }); + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(data)); + }); - return; - } + return; } res.setHeader('Content-Type', 'text/html'); diff --git a/tests/formDataServer.test.js b/tests/formDataServer.test.js index e4ffa45..323dfbc 100644 --- a/tests/formDataServer.test.js +++ b/tests/formDataServer.test.js @@ -83,7 +83,7 @@ describe('Form Data Server', () => { }; const response = await axios.post(`${HOST}/submit-expense`, expense); - expect(response.headers['content-type']).toBe('application/json'); + expect(response.headers['content-type']).toBe('text/html'); expect(response.data).toStrictEqual(expense); });