Skip to content

Solution#197

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

Solution#197
ivan-major wants to merge 3 commits intomate-academy:masterfrom
ivan-major:develop

Conversation

@ivan-major
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

I am requesting changes because several core requirements of the task are not yet implemented, which means a user cannot complete the flow using a normal browser form. In src/createServer.js, you only define a POST /add-expense handler; you need to add a GET '/' route (near the top of your request handler, before the POST check) that serves an HTML page containing a form with date, title, and amount inputs and method="POST" action="/add-expense". In the POST handler (around the code where you currently call JSON.parse(body)), you assume JSON input, but an HTML form sends application/x-www-form-urlencoded, so you should detect req.headers['content-type'], parse urlencoded bodies (e.g. with querystring.parse(body)), and wrap all parsing in a try/catch so that invalid input returns a 400 error instead of crashing the server. The successful response currently sends Content-Type: application/json and raw JSON; you need to change this to Content-Type: text/html and embed JSON.stringify(expense, null, 2) inside an HTML structure such as <html><body><pre>...</pre></body></html> so the browser shows a formatted JSON page as required. When writing to db/expense.json, you can improve readability by using JSON.stringify(expense, null, 2) instead of the compact form you have now. Finally, 400/404 error responses should also be consistent with the main behavior (ideally HTML pages or at least clearly formatted messages explaining which required fields are missing), so users understand what went wrong. Once these changes are made, your server will align much better with the specification and provide a complete, user-friendly form-to-JSON flow.


✨ 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 thread src/createServer.js
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'POST' && req.url === '/add-expense') {
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 requires showing an HTML form. There is no GET handler to serve the form (e.g. GET '/' returning an HTML page with inputs for date, title and amount). Add a route that serves the form HTML.

Comment thread src/createServer.js Outdated
});

req.on('end', () => {
const expense = JSON.parse(body);
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 code uses JSON.parse(body), which assumes the client sends JSON. An HTML form will typically submit application/x-www-form-urlencoded. Detect Content-Type and parse accordingly (for urlencoded use querystring.parse), and wrap parsing in try/catch to avoid crashing on invalid input.

Comment thread src/createServer.js
Comment on lines +23 to +24
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(expense));
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 response for a successful save is sent with Content-Type: application/json and raw JSON. The requirement asks to "return an HTML page with well formatted JSON" — change the response to text/html and embed pretty-printed JSON (for example JSON.stringify(expense, null, 2)) inside an HTML structure such as <pre>...</pre>.

Comment thread src/createServer.js Outdated
const path = require('path');
const dataPath = path.resolve(__dirname, '../db/expense.json');

fs.writeFileSync(dataPath, JSON.stringify(expense));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When writing the expense to file you use JSON.stringify(expense). If you want the saved file to be readable/use well formatted JSON, use JSON.stringify(expense, null, 2) to include indentation.

Comment thread src/createServer.js
Comment on lines +26 to +27
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(['Missing required fields']));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Error responses (400/404) are returned as application/json. Consider returning HTML pages for errors or at least consistent responses with the task (the main success response should be an HTML page). Also provide clearer error information when required fields are missing.

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.

Sorry, the pull request review was unsuccessful for the following reasons:

Warning

No changes were made in the pull request since the last review. Please make sure that you have added files to the pull request and committed the changes.
[CODE: NCM]

If the issue is within your control, please address it.
The next review attempt will automatically launch in about 1 hour.
If you believe this is an error or need assistance, please contact the support team.

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 solution still needs changes before it fully meets the task requirements for user-facing responses. In src/createServer.js, the successful POST /add-expense branch (after writing to db/expense.json) currently returns Content-Type: application/json with raw JSON, but the spec requires Content-Type: text/html and an HTML document that embeds JSON.stringify(expense, null, 2) (for example: <html><body><pre>...formatted JSON...</pre></body></html>), so browsers show nicely formatted JSON. The 400 response in the parsing try/catch also uses JSON; instead, keep the 400 status but send an HTML page (text/html; charset=utf-8) with a clear, human-readable error message like "There was a problem reading your form data." Similarly, in the "missing required fields" branch of the same handler, replace the JSON response with an HTML page that clearly explains which fields are missing (e.g., a <p>Missing required fields: date, title, amount</p> message). The fallback 404 handler at the bottom of createServer.js should also be updated from JSON to an HTML response (text/html; charset=utf-8) with a friendly "Not Found" message to keep all responses consistent. Once these response branches are converted from JSON to HTML with appropriate content types and clear messages, your implementation will align well with the project requirements, and the rest of your parsing and file-writing logic can shine through as it already looks solid.


✨ 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 thread src/createServer.js
Comment on lines +72 to +73
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(expense));
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 successful POST response currently sends 'Content-Type: application/json' and raw JSON. The spec requires 'Content-Type: text/html' and an HTML document embedding JSON.stringify(expense, null, 2) (for example inside a

) so the browser shows formatted JSON.

Comment thread src/createServer.js
Comment on lines +60 to +61
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(['Invalid request payload']));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When parsing fails you return a 400 with application/json. Parsing errors should still return a 400 but as an HTML page (Content-Type: text/html) with a clear, user-friendly error message so the server doesn't return raw JSON to a browser.

Comment thread src/createServer.js
Comment on lines +74 to +76
} else {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(['Missing required fields']));
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 'missing required fields' branch responds with application/json. Change this to an HTML response (Content-Type: text/html) that explains which required fields are missing (or at least a clear human-readable message) to match the app's main behavior.

Comment thread src/createServer.js Outdated
Comment on lines +80 to +81
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not Found' }));
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 fallback 404 response uses application/json. Make 404 responses consistent with the rest of the app by returning an HTML page (Content-Type: text/html) with a friendly 'Not Found' message.

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

Your solution is approved as an educational override so you can keep progressing while you tighten up a few remaining details. You’ve done well implementing the HTML form on GET / and correctly parsing and saving expense data to db/expense.json, which covers the core of the task. The main issues are with response formats in src/createServer.js: in the POST /add-expense parsing error catch branch (around the try/catch for reading the body), you still return JSON instead of an HTML 400 page with Content-Type: text/html; charset=utf-8 and a clear message like “There was a problem reading your form data.” [CHECKLIST ITEM #6, #12]. In the successful POST /add-expense branch after fs.writeFileSync, the response is application/json, but the requirement is an HTML document (Content-Type: text/html or text/html; charset=utf-8) that embeds JSON.stringify(expense, null, 2) inside something like a <pre> block so the saved expense is shown nicely formatted. [CHECKLIST ITEM #5, #11, #16]. In the “missing required fields” branch of that same handler, you currently send JSON; instead, compute which fields (e.g., date, title, amount) are missing and return a 400 HTML page (text/html; charset=utf-8) with a message such as <p>Missing required fields: date, title</p> listing them explicitly. [CHECKLIST ITEM #7, #19]. As a minor polish, making all HTML responses consistently include charset=utf-8 in the Content-Type header will keep your server behavior uniform. Overall, you’re very close—your data handling is solid, and once you adjust these response branches to return the required HTML, your implementation will fully match the spec.


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

Footnotes

  1. Rate AI review example

Comment thread src/createServer.js
Comment on lines +59 to +63
} catch (error) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(['Invalid request payload']));

return;
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 parsing error branch currently returns JSON. Per checklist items #6 and #12 you must keep the 400 status but send an HTML page (Content-Type: 'text/html; charset=utf-8') with a clear human-readable message such as "There was a problem reading your form data." Replace the JSON response here with such an HTML response.

Comment thread src/createServer.js
Comment on lines +71 to +73
fs.writeFileSync(dataPath, JSON.stringify(expense, null, 2));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(expense));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After writing the expense file the handler returns application/json. Per checklist items #5, #11 and #16 the successful POST /add-expense response must use Content-Type: 'text/html' and return an HTML document that embeds JSON.stringify(expense, null, 2) (for example inside a <pre>). Replace this JSON response with an HTML document containing the pretty-printed JSON.

Comment thread src/createServer.js
Comment on lines +74 to +76
} else {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(['Missing required fields']));
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 "missing required fields" branch returns JSON. Per checklist item #7 change this to an HTML page (Content-Type: 'text/html; charset=utf-8') that clearly explains which fields are missing (for example: <p>Missing required fields: date, title, amount</p>). Consider computing the missing field names from expense and inserting them into the message.

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