Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 72 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,78 @@
'use strict';

const fs = require('fs');
const path = require('path');
const http = require('http');

const requestHandler = (req, res) => {
if (req.method === 'GET' && req.url === '/') {
const filePath = path.join(__dirname, 'index.html');

fs.readFile(filePath, (err, data) => {
if (err) {
res.statusCode = 500;
res.end('Internal Server Error');

return;
}

res.setHeader('Content-Type', 'text/html');
res.end(data);
});

return;
}

if (req.method === 'POST' && req.url === '/add-expense') {
const filePath = path.join(__dirname, '../db/expense.json');
let body = '';

req.on('data', (chunk) => {
body += chunk;
});

req.on('end', () => {
let expense;

try {
const contentType = req.headers['content-type'];

if (contentType.includes('application/json')) {
expense = JSON.parse(body);
} else {
const parsed = new URLSearchParams(body);

expense = Object.fromEntries(parsed.entries());
}
} catch {
res.statusCode = 400;
res.end('Invalid JSON');

return;
}

if (!expense.date || !expense.title || !expense.amount) {
res.statusCode = 400;
res.end('Missing required fields');

return;
}

fs.writeFileSync(filePath, JSON.stringify(expense, null, 2));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The current implementation overwrites the entire 'expense.json' file with each new expense. This means only the most recent expense will be saved. If the intention is to keep a list of expenses, you should read the existing file, parse it as an array, push the new expense, and then write the updated array back to the file.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: This line overwrites the entire expense.json file with only the latest expense object. According to the requirements, you should store all expenses as an array. To fix this, read the existing file (if it exists), parse it as an array (or initialize an empty array if the file is empty or missing), append the new expense, and then write the updated array back to the file. This is a critical issue that prevents your server from tracking multiple expenses.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The current logic overwrites the entire expense.json file with only the latest expense, instead of appending the new expense to a list. According to the requirements, all submitted expenses should be preserved in an array. Please modify this section to read the existing expenses, append the new one, and then write the updated array back to the file.


res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(expense));
});

return;
}

res.statusCode = 404;
res.end('Not found');
};

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer(requestHandler);
}

module.exports = {
Expand Down
15 changes: 15 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/add-expense" method="POST">
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The form uses the default submission method, which sends data as 'application/x-www-form-urlencoded'. Your server expects JSON in the POST body, so this will not work. You need to either update the server to handle form-encoded data or use JavaScript to intercept the form submission and send the data as JSON using fetch or XMLHttpRequest.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The form submits data as application/x-www-form-urlencoded by default, but your server expects JSON in the request body. To fix this, you should either:

  • Use JavaScript to handle the form submission and send the data as JSON using fetch, or
  • Update your server to parse form-encoded data.

Without this change, expense submissions from this form will not be processed correctly by your server.

<label>Date: <input type="date" name="date" required /></label><br/>
<label>Title: <input type="text" name="title" required /></label><br/>
<label>Amount: <input type="number" name="amount" required /></label><br/>А
<button type="submit">Submit</button>
</body>
</html>