diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..d7d3192 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,90 @@ '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 === '/' && 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) => { + chunks.push(chunk); + }); + + request.on('end', () => { + try { + 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; + response.end('Invalid data'); + + return; + } + + 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 || ''; + + if (accept.includes('text/html')) { + response.statusCode = 200; + response.setHeader('Content-Type', 'text/html'); + + 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');
+ }
+ });
+ } else {
+ response.statusCode = 404;
+ response.end('Not Found');
+ }
+ });
}
module.exports = {
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 @@
+
+
+
+
+
+