Skip to content
Open
Changes from 1 commit
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
55 changes: 53 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,59 @@
'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(async (req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);

if (req.method === 'POST' && url.pathname === '/add-expense') {
const chunks = [];

for await (const chunk of req) {
chunks.push(chunk);
}

const formData = Buffer.concat(chunks).toString();

let expense;

try {
expense = JSON.parse(formData);
} catch (err) {
res.statusCode = 400;

return res.end('Invalid JSON format');
}

const { date, title, amount } = expense;

if (!date || !title || !amount) {
res.statusCode = 400;

return res.end('Missing required fields');
}

const filePath = path.join(__dirname, '../db/expense.json');

try {
fs.writeFileSync(filePath, JSON.stringify(expense, null, 2), 'utf8');
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 code overwrites the entire 'expense.json' file with the new expense object each time a POST request is made. This means only the latest expense is saved, and all previous data is lost. If the task requires storing multiple expenses, you should read the existing data, append the new expense, and then write the updated array back to the file.

} catch (err) {
res.statusCode = 500;

return res.end('Failed to save data');
}

res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');

return res.end(JSON.stringify(expense, null, 2));
Comment on lines +11 to +68
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 POST /add-expense handler expects the request body to be JSON, but the HTML form on / submits data as application/x-www-form-urlencoded by default. This will cause the handler to always return 'Invalid JSON format' when submitting via the form. You need to parse form-encoded data in addition to JSON to accept submissions from the HTML form.

}

res.statusCode = 404;
res.end('Not Found');
Comment on lines +86 to +87
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 server does not serve an HTML form for submitting expenses, which is a core requirement of the task. You should add a handler for GET requests (e.g., to /) that responds with an HTML form allowing users to enter expense details and submit them.

});
}

module.exports = {
Expand Down