-
Notifications
You must be signed in to change notification settings - Fork 513
Solution #462
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 #462
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,9 +1,44 @@ | ||
| /* eslint-disable no-console */ | ||
| 'use strict'; | ||
|
|
||
| const http = require('node:http'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const parts = req.url | ||
| .split('?')[0] | ||
| .replaceAll('//', '/') | ||
| .replaceAll('/', ',') | ||
| .split(','); | ||
|
|
||
| const myURL = new URL(req.url, 'http://localhost'); | ||
| const newSearchParams = new URLSearchParams(myURL.searchParams); | ||
|
|
||
| const responce = {}; | ||
|
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. You have a small typo here and in a few other places ( |
||
|
|
||
| responce['parts'] = []; | ||
|
|
||
| if (parts.length > 0) { | ||
| for (const item of parts) { | ||
| if (item !== '') { | ||
| responce['parts'].push(item); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| responce['query'] = {}; | ||
|
|
||
| if (newSearchParams.size > 0) { | ||
| for (const [name, value] of newSearchParams) { | ||
| responce['query'][name] = value; | ||
| } | ||
| } | ||
|
Comment on lines
+33
to
+37
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 loop works perfectly fine. For future reference, there's a more concise way to achieve the same result using responce['query'] = Object.fromEntries(newSearchParams); |
||
|
|
||
| res.setHeader('Content-Type', 'application/json'); | ||
| res.end(JSON.stringify(responce)); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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.
This multi-step string manipulation works, but there's a more standard and readable way to parse URL path parts. You could split the path by
/and then filter out the resulting empty strings.Since you're already creating a
URLobject on line 16, you can get thepathnamefrom it and process it like this:This would make the logic for getting
partsmuch simpler.