From 024de8cc8134df059de6965fe04dba23729bc6cf Mon Sep 17 00:00:00 2001 From: Alina Luchytska Date: Mon, 2 Mar 2026 17:11:16 +0200 Subject: [PATCH 1/3] add solution --- src/createServer.js | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..5b783fb 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,71 @@ 'use strict'; +const http = require('http'); +const path = require('path'); +const fs = require('fs'); + function createServer() { /* Write your code here */ // Return instance of http.Server class + return http.createServer((req, res) => { + const { url } = req; + + if (!url.startsWith('/file')) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Use /file/ to load files'); + + return; + } + + if (!url.startsWith('/file/')) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + + return res.end('Use /file/ to load files'); + } + + const publicDir = path.resolve(__dirname, '..', 'public'); + let filePath = url.slice(6); + + if (filePath === '' || filePath === '/') { + filePath = 'index.html'; + } + + if (filePath.includes('..')) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Bad Request'); + + return; + } + + if (filePath.includes('//')) { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Bad Request'); + + return; + } + + const fullPath = path.join(publicDir, filePath); + + if (!fullPath.startsWith(publicDir)) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Bad Request'); + + return; + } + + fs.readFile(fullPath, (err, data) => { + if (err) { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('File not found'); + + return; + } + + res.writeHead(200); + res.end(data); + }); + }); } module.exports = { From 5fdb01771744f5cb320f4d4f78a83f963f3f9e57 Mon Sep 17 00:00:00 2001 From: Alina Luchytska Date: Tue, 3 Mar 2026 12:52:34 +0200 Subject: [PATCH 2/3] fix solution --- src/createServer.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 5b783fb..4ce9c19 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -18,8 +18,7 @@ function createServer() { } if (!url.startsWith('/file/')) { - res.statusCode = 200; - res.setHeader('Content-Type', 'text/plain'); + res.writeHead(200, { 'Content-Type': 'text/plain' }); return res.end('Use /file/ to load files'); } From a1e60dc92bf0c468302a0e9415a86dcc16ec3db4 Mon Sep 17 00:00:00 2001 From: Alina Luchytska Date: Wed, 4 Mar 2026 00:04:27 +0200 Subject: [PATCH 3/3] fix solution 2.0 --- src/createServer.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 4ce9c19..c68ab72 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -9,22 +9,30 @@ function createServer() { // Return instance of http.Server class return http.createServer((req, res) => { const { url } = req; + const publicPathPrefix = '/file'; + const pathname = url; - if (!url.startsWith('/file')) { - res.writeHead(400, { 'Content-Type': 'text/plain' }); - res.end('Use /file/ to load files'); + let relativeFilePath; - return; - } + if (pathname === publicPathPrefix || pathname === `${publicPathPrefix}/`) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + relativeFilePath = 'index.html'; + } else if (pathname.startsWith(`${publicPathPrefix}/`)) { + relativeFilePath = pathname.substring(publicPathPrefix.length + 1); + } else { + res.statusCode = 400; + res.setHeader('Content-Type', 'text/plain'); - if (!url.startsWith('/file/')) { - res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end( + 'Invalid file request. Files must be requested via /file/path/to/file.', + ); - return res.end('Use /file/ to load files'); + return; } const publicDir = path.resolve(__dirname, '..', 'public'); - let filePath = url.slice(6); + let filePath = relativeFilePath; if (filePath === '' || filePath === '/') { filePath = 'index.html';