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
74 changes: 71 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
// Write code here
// Also, you can create additional files in the src folder
// and import (require) them here
const http = require('http');
const { convertToCase } = require('./convertToCase/convertToCase.js');
const SUPPORTED_CASES = ['SNAKE', 'KEBAB', 'CAMEL', 'PASCAL', 'UPPER'];

function createServer() {
const server = http.createServer((req, res) => {
if (req.method !== 'GET') {
res.statusCode = 404;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: '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.

The task requires that all server responses have a Content-Type header for JSON. This error response is missing the res.setHeader('Content-Type', 'application/json') call.


return;
}

const myUrl = new URL(req.url, `http://${req.headers.host}`);
const text = decodeURIComponent(myUrl.pathname.slice(1));
const toCase = myUrl.searchParams.get('toCase');
const errors = [];

if (!text) {
errors.push({
message: `Text to convert is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".`,
});
}

if (!toCase) {
res.statusCode = 400;
res.statusMessage = 'Bad request';
res.setHeader('Content-Type', 'application/json');
Comment on lines +27 to +29
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This status code and header are set here, but they are also set for all errors in the if (errors.length > 0) block (lines 41-42). This makes these two lines redundant. You can remove them to make the code cleaner, as the errors are handled comprehensively later.

Comment on lines +27 to +29
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great job collecting all errors before sending a response! However, setting the status code, status message, and header here is redundant because this is already handled in the if (errors.length > 0) block on lines 43-45. It's best practice to separate the validation logic (collecting errors) from the response logic (sending the response) to keep the code cleaner and avoid repetition.


errors.push({
message: `"toCase" query param is required. Correct request is: "/<TEXT_TO_CONVERT>?toCase=<CASE_NAME>".`,
});
} else if (!SUPPORTED_CASES.includes(toCase.toUpperCase())) {
errors.push({
message:
'This case is not supported. Available cases: ' +
'SNAKE, KEBAB, CAMEL, PASCAL, UPPER.',
});
}

if (errors.length > 0) {
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.

The task requires sending a Bad request status text along with the 400 status code. You can set this using res.statusMessage = 'Bad request';.

res.statusMessage = 'Bad request';
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ errors }));

return;
}

const toCaseRes = toCase.toUpperCase();
const result = convertToCase(text, toCaseRes);

const response = {
originalText: text,
targetCase: toCaseRes,
...result,
};

res.statusCode = 200;
res.statusMessage = 'OK';
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(response));
});

return server;
}

module.exports = {
createServer,
};
6 changes: 6 additions & 0 deletions src/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// const axios = require('axios');
// const url = 'http://localhost:5700/hello?toCase=upper';

// axios.get(url)
// .then(res => console.log(res.data))
// .catch(err => console.log(err.message));
Loading