From d7fe29aa6284decd31c36091d21db8429f2b72e2 Mon Sep 17 00:00:00 2001 From: adonch Date: Fri, 27 Mar 2026 20:40:47 +0200 Subject: [PATCH 1/3] feat: implement HTTP server with request handling - Add createServer function to handle incoming requests. - Parse URL and extract pathname and search parameters. - Return JSON response for valid requests and 404 for not found. --- src/createServer.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/createServer.js b/src/createServer.js index 1fc5f4e..f6cfb11 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,9 +1,31 @@ /* 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 url = new URL(req.url, `http://${req.headers.host}`); + const pathname = url.pathname.slice(1).split('/'); + + const searchParams = url.searchParams; + + if (pathname.length > 0 || searchParams.size > 0) { + res.writeHead(200, { 'Content-Type': 'application/json' }); + + res.end( + JSON.stringify({ + parts: pathname, + query: Object.fromEntries(searchParams), + }), + ); + } else { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Not Found'); + } + }); } module.exports = { From f98f8480e8295d3d19614498ccd59708ad333923 Mon Sep 17 00:00:00 2001 From: adonch Date: Sat, 28 Mar 2026 10:35:14 +0200 Subject: [PATCH 2/3] fix: enhance request validation for server paths - Add checks for invalid paths using .. and // in the request URL. - Return 404 status with appropriate messages for invalid paths. --- src/createServer.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/createServer.js b/src/createServer.js index f6cfb11..fc9b8d9 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -12,6 +12,20 @@ function createServer() { const searchParams = url.searchParams; + if (req.url.includes('..')) { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Invalid path'); + + return; + } + + if (pathname.includes('//')) { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Not Found'); + + return; + } + if (pathname.length > 0 || searchParams.size > 0) { res.writeHead(200, { 'Content-Type': 'application/json' }); From 5887a550659c4423b3644816c5109927b7715f07 Mon Sep 17 00:00:00 2001 From: adonch Date: Sat, 28 Mar 2026 10:46:22 +0200 Subject: [PATCH 3/3] refactor: simplify request handling in createServer function - Removed redundant path validation checks. - Streamlined response handling for valid requests. - Improved readability of pathname processing. --- src/createServer.js | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index fc9b8d9..db84398 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -8,37 +8,21 @@ function createServer() { // Return instance of http.Server class return http.createServer((req, res) => { const url = new URL(req.url, `http://${req.headers.host}`); - const pathname = url.pathname.slice(1).split('/'); + const pathname = url.pathname + .slice(1) + .split('/') + .filter((item) => item.length > 0); const searchParams = url.searchParams; - if (req.url.includes('..')) { - res.writeHead(404, { 'Content-Type': 'text/plain' }); - res.end('Invalid path'); + res.writeHead(200, { 'Content-Type': 'application/json' }); - return; - } - - if (pathname.includes('//')) { - res.writeHead(404, { 'Content-Type': 'text/plain' }); - res.end('Not Found'); - - return; - } - - if (pathname.length > 0 || searchParams.size > 0) { - res.writeHead(200, { 'Content-Type': 'application/json' }); - - res.end( - JSON.stringify({ - parts: pathname, - query: Object.fromEntries(searchParams), - }), - ); - } else { - res.writeHead(404, { 'Content-Type': 'text/plain' }); - res.end('Not Found'); - } + res.end( + JSON.stringify({ + parts: pathname, + query: Object.fromEntries(searchParams), + }), + ); }); }