Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
35 changes: 19 additions & 16 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
60 changes: 60 additions & 0 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
'use strict';

const http = require('http');
const fs = require('fs');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
const filePath = req.url;

if (filePath.includes('../')) {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('path should not contain ..');

return;
} else if (filePath.includes('//')) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Invalid path');

return;
} else if (filePath === '/file') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');

res.end(
'path to file should start with /file/ and include the file name',
);

return;
} else if (!filePath.startsWith('/file/')) {
res.statusCode = 400;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the requirements, any request whose pathname does NOT start with /file/ must return the hint message with status code 200 and Content-Type: text/plain. Here you set res.statusCode = 400. Change this to 200 so the response matches the spec and tests.

res.setHeader('Content-Type', 'text/plain');
res.end('path to file should start with /file/');
Comment on lines +33 to +36
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch for requests that don't start with /file/ sets res.statusCode = 400. The requirements ask to return a hint message (text/plain) for such paths — typically with a 200 status. Change the status code here to return a hint response consistent with the spec/examples.

Comment on lines +33 to +36
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch for paths that do not start with /file/ sets res.statusCode = 400. Per the task requirements (hint for non-/file/ path), this should respond with a hint message in text/plain and status 200 (not 400). Please change the status code to 200 while keeping the hint text.


return;
}

const relativePath = filePath.slice('/file/'.length);
let fileData;

try {
if (relativePath === '') {
fileData = fs.readFileSync('./public/index.html');
} else {
fileData = fs.readFileSync(`./public/${relativePath}`);
}
} catch (error) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end(error.message);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On file-not-found you send error.message from the filesystem. That leaks an implementation-specific message (like ENOENT). The requirement only mandates a 404 with text/plain; consider returning a clearer, consistent message such as 'File not found'.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the catch block you set res.statusCode = 404 but send error.message back to the client. Do not expose internal error text; replace the response body with a consistent message such as File not found and keep Content-Type: text/plain and status 404.


return;
}
res.statusCode = 200;

if (filePath.endsWith('.html')) {
res.setHeader('Content-Type', 'text/html');
} else if (filePath.endsWith('.css')) {
res.setHeader('Content-Type', 'text/css');
}
res.end(fileData);
});
}

module.exports = {
Expand Down
Loading