Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/* eslint-disable no-console */
'use strict';

const http = require('http');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
const normalizedUrl = new URL(req.url, `http://${req.headers.host}`);
const parts = normalizedUrl.pathname.slice(1).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.

This line correctly splits the path by /, but it doesn't account for cases with consecutive slashes (e.g., /hello//world) or a trailing slash (e.g., /hello/), which will produce empty strings in the parts array. The requirements state that doubled slashes should be ignored, which implies these empty parts should be removed.

const query = Object.fromEntries(normalizedUrl.searchParams.entries());

console.log('Received request:', { parts, query });

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ parts, query }));
});
}

module.exports = {
Expand Down