Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
95 changes: 93 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,99 @@
'use strict';

const http = require('node:http');
const path = require('node:path');
const fsp = require('node:fs/promises');

const mimeType = {
'.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',
'.ico': 'image/x-icon',
'.txt': 'text/plain',
};

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer(async (req, res) => {
if (req.url.includes('..')) {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');

return res.end('Forbidden');
}

const url = new URL(req.url, `http://${req.headers.host}`);

if (!url.pathname.startsWith('/file/')) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Use /file/<path> to load files');

return;
}

const requestPath = url.pathname.replace(/^\/file\/?/, '') || 'index.html';

if (requestPath.includes('//')) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');

return res.end('Not Found');
}

const basePath = path.resolve(__dirname, '..', 'public');
const pathToFile = path.resolve(basePath, requestPath);

if (!pathToFile.startsWith(basePath + path.sep)) {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.statusMessage = 'Forbidden';
res.end('Forbidden');

return;
}

try {
const checkFile = await fsp.stat(pathToFile);

if (!checkFile.isFile()) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.statusMessage = 'Not Found';
res.end('Not a file');

return;
}
} catch {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.statusMessage = 'Not Found';
res.end('File not found');

return;
}

const ext = path.extname(pathToFile).toLowerCase();
const contentType = mimeType[ext] || 'text/plain';

try {
const file = await fsp.readFile(pathToFile);

res.statusCode = 200;
res.setHeader('Content-Type', contentType);
res.statusMessage = 'OK';
res.end(file);
} catch {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.statusMessage = 'Server Problem';
res.end();
}
});
}

module.exports = {
Expand Down
Loading