Skip to content
Open
Changes from all commits
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
40 changes: 38 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down