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
19 changes: 17 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/* eslint-disable no-console */
'use strict';

const http = require('http');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = http.createServer((req, res) => {
const fullUrl = new URL(req.url, 'http://localhost');
const fullPath =
fullUrl.host === 'localhost'
? fullUrl.pathname
: `/${fullUrl.host}${fullUrl.pathname}`;
Comment on lines +9 to +12

Choose a reason for hiding this comment

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

This logic for constructing fullPath is a bit complex and could lead to incorrect behavior. If req.url were an absolute URL (which can happen with proxy requests), the hostname would be incorrectly included in the parts array. You can simplify this by using fullUrl.pathname directly, as the URL constructor already handles this parsing correctly.

const parts = fullPath.split('/').filter(Boolean);
const query = Object.fromEntries(fullUrl.searchParams);

res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ parts, query }));
});

return server;
}

module.exports = {
Expand Down