Skip to content

task#182

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

task#182
iryna-knyzh wants to merge 3 commits intomate-academy:masterfrom
iryna-knyzh:develop

Conversation

@iryna-knyzh
Copy link

No description provided.

Copy link

@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 setting up the server to handle the POST request! However, I'm requesting a few changes as some core requirements are not yet met.

Here are the key issues to address:

  1. Missing HTML Form: The server needs to handle GET requests to serve the HTML form to the user. Currently, it only responds to POST requests.
  2. Data Overwriting: fs.writeFile replaces the content of db/expense.json on every submission. The application should append new expenses to a list within the file, not overwrite it.
  3. Asynchronous Handling: The server sends a success response before fs.writeFile completes. The response logic should be moved inside the callback of fs.writeFile to handle the result of the file operation, including potential errors.
  4. Incorrect Response Format: The server should return an HTML page containing the submitted JSON, but it's currently sending a raw JSON response (application/json).

Addressing these points will get your expense tracker working as expected. Keep up the great work!


✨ 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

/* Write your code here */
// Return instance of http.Server class
const server = http.createServer((req, res) => {
if (req.method !== 'POST' || req.url !== '/add-expense') {

Choose a reason for hiding this comment

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

The requirements state that the application should show an HTML form. This server logic only accepts POST requests to /add-expense and returns a 404 Not Found for all other requests, including GET requests that would typically be used to serve the form.

Comment on lines 37 to 44
fs.writeFile(dataPath, body, (error) => {
if (error) {
}
});

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

Choose a reason for hiding this comment

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

This block has several issues related to asynchronous operations and task requirements:

  1. Race Condition: fs.writeFile is asynchronous. The response is sent on lines 42-44 without waiting for the file to be written. All response logic, for both success and error, should be moved inside this callback.
  2. Missing Error Handling: The error object is not handled on line 38. If writing to the file fails, the server will still send a 200 OK response. You should handle this case and send an appropriate error response (e.g., status 500).
  3. Incorrect Response Format: The task requires returning an "HTML page with well formatted JSON". This code sends a raw JSON response with Content-Type: application/json. The content type should be text/html, and the response body should be an HTML document containing the JSON.

Copy link

@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 implementing the GET request handler and fixing the asynchronous response logic! You're very close to finishing this task, but a couple of changes are needed to meet the core requirements.

  1. Data is being overwritten: The db/expense.json file is overwritten on every submission, losing all previous entries. To fix this, you need to first read the file, parse the existing JSON into an array, add the new expense to it, and then write the entire updated array back to the file.
  2. Incorrect response format: The server currently returns raw JSON. The task requires returning an HTML page that displays the submitted data. Please set the Content-Type header to text/html and wrap the JSON string in <pre> tags to format it correctly in the browser.

Addressing these two points will complete the functionality. Keep up the great work!


✨ 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

return;
}

fs.writeFile(dataPath, JSON.stringify(expense), (err) => {

Choose a reason for hiding this comment

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

This line overwrites the expense.json file on every submission, causing previous data to be lost. To save all expenses, you need to first read the existing data from the file, parse it into an array, push the new expense object into that array, and then write the entire updated array back to the file.

Comment on lines 75 to 76
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(expense));

Choose a reason for hiding this comment

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

The task requires returning an HTML page with well-formatted JSON, but the current implementation returns a raw JSON response. You should set the Content-Type to text/html and wrap the JSON string in HTML tags, like <pre>, to display it on a webpage.

Copy link

@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

I am approving this submission to help you keep moving forward, but there are a few important adjustments needed to fully meet the requirements. Great job fixing the data persistence issue! The server now correctly appends new expenses. The main thing to fix is the server's response in src/createServer.js. It should return an HTML page containing the JSON, which you can do by wrapping the JSON string in <pre> tags. Additionally, your tests in tests/formDataServer.test.js should be updated to verify the append logic and to expect an HTML string in the response, not a JSON object. You're very close to a complete solution!


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

Footnotes

  1. Rate AI review example

res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');

res.end(JSON.stringify(expense, null, 2));

Choose a reason for hiding this comment

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

Great job setting the Content-Type header to text/html. However, the task requires returning an HTML page that contains the well-formatted JSON, not just the raw JSON string. To ensure the JSON is displayed correctly in the browser as specified, please wrap it in <pre> tags.

const savedData = JSON.parse(fs.readFileSync(dataPath));

expect(savedData).toStrictEqual(expense);
expect(savedData).toStrictEqual([expense]);

Choose a reason for hiding this comment

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

This test correctly verifies that a single expense is saved. To fully confirm that the data overwriting issue from the previous review is fixed, consider adding another test case that checks if a new expense is appended to a file that already contains existing expenses.


expect(response.headers['content-type']).toBe('application/json');
expect(response.headers['content-type']).toBe('text/html');
expect(response.data).toStrictEqual(expense);

Choose a reason for hiding this comment

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

This assertion doesn't align with the task requirements. The server is expected to return an HTML page containing the JSON (ideally wrapped in <pre> tags), so the response data will be a string. This test incorrectly expects a JavaScript object. It should be updated to verify that the response is the correct HTML string.

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