Skip to content

solution#188

Open
ZAKANDRE wants to merge 3 commits intomate-academy:masterfrom
ZAKANDRE:develop
Open

solution#188
ZAKANDRE wants to merge 3 commits intomate-academy:masterfrom
ZAKANDRE:develop

Conversation

@ZAKANDRE
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

This submission requires a couple of changes to meet the task requirements. You've done a great job setting up the server and parsing the form data. However, there are two key issues in src/createServer.js that need to be addressed.

  1. Data is overwritten: The db/expense.json file is overwritten on every submission, losing all previous data. The goal is to accumulate expenses. To fix this, you should first read the existing content of the file, add the new expense to the array, and then write the entire updated array back to the file.
  2. Incorrect response format: The server returns raw JSON, but the requirement is to return an HTML page that displays the JSON. You'll need to set the Content-Type header to text/html and send back an HTML string that includes the formatted JSON, for example, inside a <pre> tag.

✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment on lines +9 to +12
<form action="/add-expense"
method="POST"
enctype="multipart/form-data"
>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This form is set up correctly to send data. However, the backend logic in createServer.js that handles this submission has two issues that violate the task requirements:

  1. The db/expense.json file is overwritten on every submission, losing all previous data. It should accumulate expenses in an array.
  2. The server returns raw JSON, but the requirement is to return an 'HTML page with well formatted JSON'.

Please adjust the server-side code to fix these issues.

Comment on lines +46 to +50
let expenses = {};

expenses = { date, title, amount };

fs.writeFileSync(filePath, JSON.stringify(expenses, 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.

This logic overwrites db/expense.json on every submission. To save a history of expenses, you should read the current contents of the file, add the new expense to the list, and then write the entire list back. Remember to handle cases where the file might not exist or be empty initially.

Comment on lines +52 to +54
res.writeHead(200, { 'Content-Type': 'application/json' });

return res.end(JSON.stringify(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.

The task asks to 'return an HTML page with well formatted JSON'. This implementation returns a raw JSON response (application/json). To meet the requirement, you should set the Content-Type to text/html and send back an HTML string that includes the formatted JSON, perhaps inside a <pre> tag.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Great job fixing the data accumulation logic! Your solution is very close to complete. However, one change is needed before this can be approved. While the response body for the POST request is correctly formatted as an HTML string, the Content-Type header is set to application/json. This prevents the browser from rendering the response as an HTML page, which is a core requirement. Please update the header in src/createServer.js to text/html to ensure the browser displays the page correctly.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example


fs.writeFileSync(filePath, JSON.stringify(expenses, null, 2));

res.writeHead(200, { 'Content-Type': 'application/json' });
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You're sending an HTML string in the response body, which is correct according to the requirements. However, this Content-Type header tells the browser it's receiving JSON. To fully meet the requirement of returning an HTML page, this header should be set to text/html.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Excellent work on this task! You've successfully fixed the Content-Type header, and the server's core functionality is spot on. I am approving your solution as an educational override to help you keep moving forward. There is just one minor improvement needed to fully meet all requirements: the JSON returned in the HTML response should be pretty-printed for better readability. You can achieve this in src/createServer.js by using the space argument in JSON.stringify, just like you did when writing to the file (e.g., JSON.stringify(expenses, null, 2)). Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants