From 6398ce79d08f16091cb5daae6a093446e2dfba7c Mon Sep 17 00:00:00 2001 From: Yan Kazymyr Date: Fri, 3 Apr 2026 22:59:12 +0300 Subject: [PATCH 1/2] Solution --- src/createServer.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..898697c 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,48 @@ '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 = new URL(req.url, `http://${req.headers.host}`); + + if (url.pathname === '/file') { + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('Please specify a file path after /file/.'); + + return; + } + + if (!url.pathname.startsWith('/file')) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Use /file/ to load files'); + + return; + } + + const relativePath = url.pathname.replace(/^\/file\/?/, '') || 'index.html'; + const publicDir = path.resolve(__dirname, '..', 'public'); + const fullPath = path.resolve(publicDir, relativePath); + + if (!fullPath.startsWith(publicDir + path.sep) && fullPath !== publicDir) { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Not Found'); + + return; + } + + try { + const data = fs.readFileSync(fullPath); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(data); + } catch (err) { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Not Found'); + } + }); } module.exports = { From d0af12822bd00bffa79ca1715c83b04eb05852a7 Mon Sep 17 00:00:00 2001 From: Yan Kazymyr Date: Fri, 3 Apr 2026 23:22:46 +0300 Subject: [PATCH 2/2] Fix --- src/createServer.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/createServer.js b/src/createServer.js index 898697c..f73767c 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -4,6 +4,24 @@ const http = require('http'); const path = require('path'); const fs = require('fs'); +const MIME_TYPES = { + '.html': 'text/html', + '.css': 'text/css', + '.js': 'application/javascript', + '.json': 'application/json', + '.png': 'image/png', + '.jpg': 'image/jpeg', + '.jpeg': 'image/jpeg', + '.svg': 'image/svg+xml', + '.txt': 'text/plain', +}; + +function getMimeType(filePath) { + const ext = path.extname(filePath).toLowerCase(); + + return MIME_TYPES[ext] || 'application/octet-stream'; +} + function createServer() { return http.createServer((req, res) => { const url = new URL(req.url, `http://${req.headers.host}`); @@ -35,8 +53,9 @@ function createServer() { try { const data = fs.readFileSync(fullPath); + const contentType = getMimeType(fullPath); - res.writeHead(200, { 'Content-Type': 'text/html' }); + res.writeHead(200, { 'Content-Type': contentType }); res.end(data); } catch (err) { res.writeHead(404, { 'Content-Type': 'text/plain' });