Skip to content

Solution for params echo server#424

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

Solution for params echo server#424
hma-3 wants to merge 2 commits intomate-academy:masterfrom
hma-3:develop

Conversation

@hma-3
Copy link
Copy Markdown

@hma-3 hma-3 commented Sep 10, 2025

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

Approved. Your server meets the core requirements: correct HTTP setup, clean path splitting with empty segments removed, robust query parsing with URLSearchParams, proper JSON serialization, and the Content-Type header is set. The behavior for empty/missing query and doubled slashes is handled well, and returning exactly { parts, query } is consistent.

Minor improvements (non-blocking):

  • Consider setting res.statusCode = 200 explicitly for clarity (checklist 3.9).
  • If tests/consumers expect decoded path segments, map decodeURIComponent over parts and guard with try/catch to avoid crashes on malformed encodings (checklist 3.4).
  • Optional: defensively default const url = req.url || '/' before splitting.

Nice work—concise and idiomatic implementation. Keep it up!


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

Footnotes

  1. Rate AI review example

/* eslint-disable no-console */
'use strict';

const http = require('http');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Import is fine. const http = require('http') is correct for creating the server in Node.js. This line supports the server creation required by the task.

/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
const [path, params] = req.url.split('?');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

URL split approach — acceptable. Using const [path, params] = req.url.split('?') works here. Note that when there is no ?, params becomes undefined, and new URLSearchParams(undefined) produces an empty set (which is fine for the task). This meets the requirement to handle missing query gracefully (checklist items 1.4 and 3.5).

// Return instance of http.Server class
return http.createServer((req, res) => {
const [path, params] = req.url.split('?');
const searchParams = new URLSearchParams(params);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Query parsing is correct but note repeated keys behavior. new URLSearchParams(params) combined with Object.fromEntries(searchParams.entries()) yields an object of string values as required. If the same key appears multiple times, the last occurrence will be kept — this is a consistent choice but document it if needed (checklist item 3.3).

const [path, params] = req.url.split('?');
const searchParams = new URLSearchParams(params);

res.setHeader('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.

Content-Type header set correctly. res.setHeader('Content-Type', 'application/json') ensures the response is treated as JSON. For clarity consider also setting res.statusCode = 200 before sending the body so the success status is explicit (checklist item 3.9).


res.end(
JSON.stringify({
parts: path.split('/').filter(Boolean),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Path splitting meets requirements but consider decoding segments. parts: path.split('/').filter(Boolean) correctly removes empty segments (doubled slashes ignored and root / -> []) as required (checklist items 1.3 and 3.6). If tests expect decoded segments (e.g. %20 -> space), map decodeURIComponent over the filtered parts. Because decodeURIComponent can throw on malformed sequences, wrap decoding in a safe helper or try/catch to avoid crashes (checklist item 3.4 & 3.10).

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

res.end(
JSON.stringify({
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Response serialization is correct. JSON.stringify({ parts: ..., query: ... }) ensures the output is valid JSON text (checklist item 3.8). The function always returns an object with both keys parts and query, even when empty, satisfying checklist item 3.5.

query: Object.fromEntries(searchParams.entries()),
}),
);
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor robustness note: If you later change how the URL is parsed (e.g. use new URL(req.url)), ensure to supply a base or handle absolute vs relative URLs. The current simple split approach is sufficient and less error-prone for the task constraints.

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.

3 participants