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
6 changes: 3 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
extends: '@mate-academy/eslint-config',
env: {
jest: true
jest: true,
},
rules: {
'no-proto': 0
'no-proto': 0,
},
plugins: ['jest']
plugins: ['jest'],
};
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
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: 55 additions & 5 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
'use strict';

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

const PUBLIC_DIR = path.resolve(__dirname, '../public');
const TEXT_PLAIN = { 'Content-Type': 'text/plain' };

function send(res, status, message) {
res.writeHead(status, TEXT_PLAIN);
res.end(message);
}

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

if (url === '/file') {
Comment thread
YevheniiKa marked this conversation as resolved.
send(res, 200, 'Use /file/<path> to load files');

return;
}
Comment thread
YevheniiKa marked this conversation as resolved.
Comment thread
YevheniiKa marked this conversation as resolved.

if (!url.startsWith('/file/')) {
Comment thread
YevheniiKa marked this conversation as resolved.
send(res, 400, 'Invalid path');

return;
}
Comment thread
YevheniiKa marked this conversation as resolved.
Comment thread
YevheniiKa marked this conversation as resolved.

const relativePath = url.slice('/file/'.length);
Comment thread
YevheniiKa marked this conversation as resolved.

if (relativePath.includes('//')) {
send(res, 404, 'File not found');

return;
}

const resolvedPath = path.resolve(PUBLIC_DIR, relativePath);

if (!resolvedPath.startsWith(PUBLIC_DIR)) {
send(res, 400, 'Invalid path');

return;
}

fs.readFile(resolvedPath, (err, data) => {
if (err) {
send(res, 404, 'File not found');

return;
}

res.writeHead(200);
res.end(data);
});
});
}

module.exports = {
createServer,
};
module.exports = { createServer };
Loading