Skip to content

added solution#193

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

added solution#193
yahohulia wants to merge 3 commits intomate-academy:masterfrom
yahohulia:develop

Conversation

@yahohulia
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

Good effort in setting up the Node.js server and HTML form. However, I am requesting changes because several critical issues are preventing the application from working as required.

Here are the key things to fix:

  1. Form Submission URL: The form in src/index.html submits to /submit, but your server in src/createServer.js listens for POST requests on /add-expense. These need to match for the submission to work.
  2. Server Errors:
    • The server tries to load index.html from an incorrect path (public/index.html). It should point to src/index.html to display the form.
    • The server incorrectly tries to parse the form data with JSON.parse(). Standard form data is x-www-form-urlencoded and requires a different parser, like Node's built-in querystring module.
  3. Incorrect Response Format: After a successful submission, the server must return an HTML page that displays the JSON, not a raw JSON response. You'll need to set the Content-Type header to text/html and send back an HTML string.

Please address these points to get the application fully functional.


✨ 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/index.html Outdated
<title>Document</title>
</head>
<body>
<form action="/submit" 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.

The action attribute of the form is set to /submit, but the server (createServer.js) is configured to handle POST requests on the /add-expense endpoint. This mismatch will cause a '404 Not Found' error upon submission. You need to update this URL to match the server's endpoint.

Comment thread src/createServer.js Outdated
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
const fileStream = fs.createReadStream('public/index.html');
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 path to index.html appears to be incorrect. Based on the provided file structure, index.html is located in the src directory, not public. This will cause an error and prevent the form from being displayed.

Comment thread src/createServer.js
req.on('data', (chunck) => (body += chunck));

req.on('end', () => {
const data = 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.

HTML forms submitted with the default enctype send data as application/x-www-form-urlencoded, not as a JSON string. Using JSON.parse() on this data will result in an error. You should use a tool like Node's built-in querystring module or the URLSearchParams class to parse the form data correctly.

Comment thread src/createServer.js
Comment on lines +46 to +47
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 task requires returning an 'HTML page with well formatted JSON'. Your implementation returns a raw JSON response. You need to change the Content-Type to text/html and construct an HTML string that includes the JSON data, perhaps inside a <pre> tag to preserve formatting.

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 form's submission URL and the path for serving index.html! The application now correctly displays the form.

However, this requires a couple of changes as two critical issues from the previous review still need to be addressed:

  1. Incorrect Form Data Parsing: In src/createServer.js, the server still uses JSON.parse() on the request body. Standard HTML forms send data in x-www-form-urlencoded format (e.g., key1=value1&key2=value2), which causes a parsing error. Please use a different method, like Node's built-in querystring module, to handle this format.

  2. Incorrect Response Format: After a successful submission, the server sends a raw JSON response. The requirement is to return an HTML page that displays the saved data. You'll need to set the Content-Type header to text/html and wrap the JSON string in HTML, perhaps using a <pre> tag to keep it nicely formatted.

You're very close to completing the task. 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

Comment thread src/createServer.js
req.on('data', (chunck) => (body += chunck));

req.on('end', () => {
const data = 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.

The body of a standard form submission is in x-www-form-urlencoded format (e.g., key1=value1&key2=value2), not JSON. Using JSON.parse() here will cause a syntax error when the form is submitted. You need to parse this URL-encoded string. Consider using Node's built-in querystring module or the URLSearchParams class.

Comment thread src/createServer.js
Comment on lines +46 to +47
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 task requires returning an HTML page that displays the saved JSON data. Currently, you're sending a raw JSON response with Content-Type: application/json. You should set the Content-Type to text/html and construct an HTML string to send back. A good way to display the JSON is inside <pre> tags to preserve formatting.

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'm approving this submission as an educational step to help you progress, but the application isn't fully functional yet due to two critical issues with how the form submission is handled. You've done an excellent job setting up the server and serving the initial form!

However, the server currently tries to parse the form data with JSON.parse(), which causes a crash because HTML forms send data in a different format (application/x-www-form-urlencoded). You'll need to use a different tool, like Node's built-in querystring module, to handle this.

Additionally, after a successful submission, the server must respond with an HTML page displaying the saved data, not raw JSON. This involves setting the Content-Type header to text/html and wrapping your JSON string in HTML tags (e.g., <pre>).

You are very close to the solution! Focus on these two areas, and you'll have it working perfectly. Keep up the great effort!


✨ 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
req.on('data', (chunck) => (body += chunck));

req.on('end', () => {
const data = 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.

The HTML form sends data in the x-www-form-urlencoded format (like date=...&title=...), not as a JSON string. Using JSON.parse() on this format will cause a syntax error.

You should use a different method to parse this kind of data. Consider using Node's built-in querystring module or the URLSearchParams class.

Comment thread src/createServer.js
Comment on lines +51 to +52
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.

According to the task description, the server must return an HTML page that displays the saved data, not a raw JSON response.

You'll need to change the Content-Type to text/html and send back an HTML string that includes the formatted JSON data (e.g., by wrapping JSON.stringify(expense, null, 2) inside a <pre> tag).

Comment thread src/index.html
<div style="margin-top: 5px">
<button type="submit">Submit</button>
</div>
</form>
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 request body from an HTML form with enctype="application/x-www-form-urlencoded" is not a JSON string. It's a URL-encoded string (e.g., key1=value1&key2=value2). Using JSON.parse() here will cause a crash.

You need to parse this specific format. Consider using Node.js's built-in querystring module or the URLSearchParams class.

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