-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Solution #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #1078
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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' })); | ||
|
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The task requires sending a |
||
| 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, | ||
| }; | ||
| 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)); |
There was a problem hiding this comment.
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-Typeheader for JSON. This error response is missing theres.setHeader('Content-Type', 'application/json')call.