Skip to content
Open
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
73 changes: 71 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,77 @@
'use strict';

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

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = http.createServer((req, res) => {
const protectedUrl = new URL(req.url, `http://${req.headers.host}`);
const url = protectedUrl.pathname;

if (url === '/file' || url === '/file/') {
const indexFilePath = path.join(__dirname, '../public/index.html');

fs.readFile(indexFilePath, (err, data) => {
if (err) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Not Found');

return;
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(data);
});

return;
}

if (!url.startsWith('/file/')) {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Bad Request');

return;
}

const relativePath = url.slice(6);

if (relativePath.includes('..')) {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Bad Request');

return;
}

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

return;
}

const filePath = path.join(__dirname, '../public', relativePath);

fs.readFile(filePath, (err, data) => {
if (err) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Not Found');

return;
}

res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end(data);
});
});

return server;
}

module.exports = {
Expand Down
Loading