From 77ca2f79ff3888198028d28b34c79208f6a314bb Mon Sep 17 00:00:00 2001 From: Victor Komara Date: Thu, 26 Feb 2026 14:31:56 +0000 Subject: [PATCH] Solution --- src/createServer.js | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 1fc5f4e..0e18bf2 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,9 +1,45 @@ /* eslint-disable no-console */ 'use strict'; +const http = require('http'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + const server = http.createServer((req, res) => { + // 1. Завжди відповідаємо JSON + res.setHeader('Content-Type', 'application/json'); + + // 2. Розбираємо URL + const [pathWithText, queryString] = req.url.split('?'); + + const textArr = pathWithText.split('/').filter((el) => el.length > 0); + + const params = new URLSearchParams(queryString); + + const objParams = Object.fromEntries(params); + + try { + const responseBody = { + parts: textArr, + query: objParams, + }; + + res.statusCode = 200; + res.statusMessage = 'OK'; + res.end(JSON.stringify(responseBody)); + } catch (err) { + console.error('Business logic error:', err.message); + + res.statusCode = 500; // Internal Server Error + + res.end( + JSON.stringify({ + errors: [{ message: 'Internal server error occurred.' }], + }), + ); + } + }); + + return server; } module.exports = {