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
70 changes: 70 additions & 0 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,78 @@
'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 } = req;
const publicPathPrefix = '/file';
const pathname = url;

let relativeFilePath;

if (pathname === publicPathPrefix || pathname === `${publicPathPrefix}/`) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When serving index.html, the Content-Type header should be text/html. Setting it to text/plain will cause browsers to display the HTML source code as plain text instead of rendering it as a webpage.

relativeFilePath = 'index.html';
} else if (pathname.startsWith(`${publicPathPrefix}/`)) {
relativeFilePath = pathname.substring(publicPathPrefix.length + 1);
} else {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');

res.end(
'Invalid file request. Files must be requested via /file/path/to/file.',
);

return;
}

const publicDir = path.resolve(__dirname, '..', 'public');
let filePath = relativeFilePath;

if (filePath === '' || filePath === '/') {
filePath = 'index.html';
}

if (filePath.includes('..')) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request');

return;
}

if (filePath.includes('//')) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Bad Request');

return;
}

const fullPath = path.join(publicDir, filePath);

if (!fullPath.startsWith(publicDir)) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Bad Request');

return;
}

fs.readFile(fullPath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('File not found');

return;
}

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

module.exports = {
Expand Down