diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..d4afec7 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,113 @@ 'use strict'; +const http = require('http'); +const fs = require('fs'); + +const form = ` +
+`; + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((req, res) => { + if (req.method === 'GET') { + if (req.url === '/') { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(form); + } else if (req.url === '/add-expense') { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Incorrect request'); + } else { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Page not found'); + } + + return; + } + + if (req.method === 'POST') { + if (req.url === '/add-expense') { + let body = ''; + + req.on('data', (chunk) => (body += chunk)); + + req.on('end', () => { + let expenseData; + + if (req.headers['content-type'] === 'application/json') { + expenseData = JSON.parse(body); + } else { + const params = new URLSearchParams(body); + + expenseData = Object.fromEntries(params); + } + + const dateObj = new Date(expenseData.date); + const isValidDate = dateObj instanceof Date && !isNaN(dateObj); + + const isValidTitle = + expenseData.title && expenseData.title.trim().length > 0; + + const amount = parseFloat(expenseData.amount); + const isValidAmount = !isNaN(amount) && amount > 0; + + if (!isValidDate || !isValidTitle || !isValidAmount) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Invalid data'); + } else { + const filePath = 'db/expense.json'; + + if (!fs.existsSync('db')) { + fs.mkdirSync('db'); + } + + if (!fs.existsSync(filePath)) { + fs.writeFileSync(filePath, JSON.stringify([])); + } + + const record = { + date: expenseData.date, + title: expenseData.title, + amount: expenseData.amount, + }; + + fs.writeFileSync(filePath, JSON.stringify(record, null, 2)); + + if (req.headers['content-type'] === 'application/json') { + // JSON запит — повертай JSON + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(record)); + } else { + // HTML форма — повертай HTML + res.writeHead(200, { 'Content-Type': 'text/html' }); + + const html = `${JSON.stringify(record, null, 2)}`;
+
+ res.end(html);
+ }
+ }
+ });
+ }
+ }
+ });
}
module.exports = {