Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
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
| /* eslint-disable no-console */ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); |
There was a problem hiding this comment.
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.
src/createServer.js
Outdated
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const [path, params] = req.url.split('?'); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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({ |
There was a problem hiding this comment.
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()), | ||
| }), | ||
| ); | ||
| }); |
There was a problem hiding this comment.
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.
No description provided.