From 86d7f233f85b9787ec4789f2235eb858a6f3f2d3 Mon Sep 17 00:00:00 2001 From: vilich Date: Mon, 5 Jan 2026 19:27:28 +0100 Subject: [PATCH 1/3] add --- src/createServer.js | 71 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..21937b0 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,75 @@ 'use strict'; +const { Server } = require('http'); +const fs = require('fs'); +const path = require('path'); + +const ALLOWED_ENDPOINTS = { + addExpense: { + route: '/add-expense', + methods: ['POST'], + }, +}; + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + const server = new Server(); + + server.on('request', (req, res) => { + const baseUrl = `http://${req.headers.host}`; + const url = new URL(req.url, baseUrl); + + if ( + url.pathname !== ALLOWED_ENDPOINTS.addExpense.route || + !ALLOWED_ENDPOINTS.addExpense.methods.includes(req.method) + ) { + res.statusCode = 404; + res.setHeader('Content-Type', 'text/plain'); + + res.end('Invalid url'); + + return; + } + + const bodyChunks = []; + + req.on('data', (chunk) => { + bodyChunks.push(chunk); + }); + + req.on('end', () => { + const json = Buffer.concat(bodyChunks); + const expense = JSON.parse(json); + + if (!expense?.date || !expense?.title || !expense?.amount) { + res.statusCode = 400; + res.end('Bad user input'); + + return; + } + + const fileStream = fs.createWriteStream( + path.normalize(path.join(__dirname, '../db/expense.json')), + ); + + fileStream.on('error', (error) => { + res.statusCode = 500; + res.setHeader('Content-Type', 'text/plain'); + /* eslint-disable-next-line no-console */ + console.error(error); + res.end('Server error'); + }); + + fileStream.on('finish', () => { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(expense, null, 2)); + }); + + fileStream.end(json); + }); + }); + + return server; } module.exports = { From 7c841cc9c5c257486148fc923e294f1e49548992 Mon Sep 17 00:00:00 2001 From: vilich Date: Mon, 5 Jan 2026 19:36:18 +0100 Subject: [PATCH 2/3] ad2 --- src/createServer.js | 61 ++++++++++++++++----------------------------- src/index.html | 17 +++++++++++++ 2 files changed, 39 insertions(+), 39 deletions(-) create mode 100644 src/index.html diff --git a/src/createServer.js b/src/createServer.js index 21937b0..6b9d4e1 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,73 +1,56 @@ 'use strict'; -const { Server } = require('http'); +const http = require('http'); const fs = require('fs'); const path = require('path'); -const ALLOWED_ENDPOINTS = { - addExpense: { - route: '/add-expense', - methods: ['POST'], - }, -}; - function createServer() { - const server = new Server(); + const server = new http.Server(); server.on('request', (req, res) => { - const baseUrl = `http://${req.headers.host}`; - const url = new URL(req.url, baseUrl); - - if ( - url.pathname !== ALLOWED_ENDPOINTS.addExpense.route || - !ALLOWED_ENDPOINTS.addExpense.methods.includes(req.method) - ) { + if (req.url !== '/add-expense') { + res.setHeader('Content-type', 'text/plain'); res.statusCode = 404; - res.setHeader('Content-Type', 'text/plain'); - - res.end('Invalid url'); + res.end('Wrong request url'); return; } - const bodyChunks = []; + const chunks = []; req.on('data', (chunk) => { - bodyChunks.push(chunk); + chunks.push(chunk); }); req.on('end', () => { - const json = Buffer.concat(bodyChunks); - const expense = JSON.parse(json); + const text = Buffer.concat(chunks).toString(); - if (!expense?.date || !expense?.title || !expense?.amount) { + const data = JSON.parse(text); + + if (!data['date'] || !data['title'] || !data['amount']) { res.statusCode = 400; - res.end('Bad user input'); + res.end('Not full data'); return; } - const fileStream = fs.createWriteStream( - path.normalize(path.join(__dirname, '../db/expense.json')), - ); + const writeStream = fs.createWriteStream(path.resolve('db/expense.json')); - fileStream.on('error', (error) => { - res.statusCode = 500; - res.setHeader('Content-Type', 'text/plain'); - /* eslint-disable-next-line no-console */ - console.error(error); - res.end('Server error'); - }); + writeStream.end(text); - fileStream.on('finish', () => { + writeStream.on('finish', () => { + res.setHeader('Content-type', 'application/json'); res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(expense, null, 2)); + res.end(text); }); + }); - fileStream.end(json); + req.on('error', (error) => { + res.statusCode = 400; + res.end(`Request error: ${error}`); }); }); + server.on('error', () => {}); return server; } diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..98e5d22 --- /dev/null +++ b/src/index.html @@ -0,0 +1,17 @@ + + + + + Add Expense + + +

Expense Form

+
+
+
+
+ +
+ + From 13c1ade391219c71ca19a844320d1f6ee99e1189 Mon Sep 17 00:00:00 2001 From: vilich Date: Mon, 5 Jan 2026 19:40:23 +0100 Subject: [PATCH 3/3] add3 --- src/createServer.js | 82 ++++++++++++++++++++++++++++----------------- src/index.html | 17 ---------- 2 files changed, 51 insertions(+), 48 deletions(-) delete mode 100644 src/index.html diff --git a/src/createServer.js b/src/createServer.js index 6b9d4e1..c23809f 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,56 +1,76 @@ +/* eslint-disable no-console */ '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 + const server = new http.Server(); server.on('request', (req, res) => { - if (req.url !== '/add-expense') { - res.setHeader('Content-type', 'text/plain'); - res.statusCode = 404; - res.end('Wrong request url'); + if (req.method === 'POST' && req.url === '/add-expense') { + let body = ''; - return; - } + req.on('data', (chunk) => { + body += chunk.toString(); + }); + + req.on('end', () => { + let fields; - const chunks = []; + try { + fields = JSON.parse(body); + } catch (err) { + res.statusCode = 400; - req.on('data', (chunk) => { - chunks.push(chunk); - }); + return res.end('Invalid JSON'); + } - req.on('end', () => { - const text = Buffer.concat(chunks).toString(); + if (!fields.amount || !fields.date || !fields.title) { + res.statusCode = 400; - const data = JSON.parse(text); + return res.end('Missing required fields'); + } - if (!data['date'] || !data['title'] || !data['amount']) { - res.statusCode = 400; - res.end('Not full data'); + const filePath = 'db/expense.json'; - return; - } + const newData = { + amount: fields.amount, + date: fields.date, + title: fields.title, + }; - const writeStream = fs.createWriteStream(path.resolve('db/expense.json')); + fs.writeFileSync(filePath, JSON.stringify(newData)); - writeStream.end(text); + const file = fs.readFileSync(filePath); - writeStream.on('finish', () => { - res.setHeader('Content-type', 'application/json'); - res.statusCode = 200; - res.end(text); + res.setHeader('Content-Type', 'application/json'); + res.end(file); }); - }); - req.on('error', (error) => { - res.statusCode = 400; - res.end(`Request error: ${error}`); - }); + return; + } + + if (req.url !== '/' && req.url !== '/add-expense') { + res.statusCode = 404; + + return res.end('Invalid Url'); + } + + res.setHeader('Content-type', 'text/html'); + + res.end( + `
+ + + + +
`, + ); }); - server.on('error', () => {}); return server; } diff --git a/src/index.html b/src/index.html deleted file mode 100644 index 98e5d22..0000000 --- a/src/index.html +++ /dev/null @@ -1,17 +0,0 @@ - - - - - Add Expense - - -

Expense Form

-
-
-
-
- -
- -