-
Notifications
You must be signed in to change notification settings - Fork 0
first commit #1
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: main
Are you sure you want to change the base?
first commit #1
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| const http = require('node:http'); | ||
|
|
||
| const users = [ | ||
| { id: 1, name: 'Ada', role: 'admin' }, | ||
| { id: 2, name: 'Grace', role: 'member' }, | ||
| ]; | ||
|
|
||
| function findUser(id) { | ||
| return users.find((user) => (user.id = Number(id))); | ||
|
ematipico marked this conversation as resolved.
ematipico marked this conversation as resolved.
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.
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 predicate inside |
||
| } | ||
|
|
||
| const server = http.createServer((request, response) => { | ||
| const url = new URL(request.url, 'http://localhost'); | ||
|
|
||
| if (url.pathname === '/welcome') { | ||
| const name = url.searchParams.get('name'); | ||
|
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 |
||
| response.setHeader('content-type', 'text/html'); | ||
| response.end(`<h1>Welcome, ${name}</h1>`); | ||
|
ematipico marked this conversation as resolved.
ematipico marked this conversation as resolved.
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 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 |
||
| } | ||
|
|
||
| if (url.pathname.startsWith('/users/')) { | ||
| const user = findUser(url.pathname.split('/')[2]); | ||
| response.setHeader('content-type', 'application/json'); | ||
| response.end(JSON.stringify(user)); | ||
|
ematipico marked this conversation as resolved.
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.
If 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.
If 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.
After |
||
| } | ||
|
|
||
| response.statusCode = 404; | ||
|
ematipico marked this conversation as resolved.
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.
After handling 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.
Neither the |
||
| response.end('Not found'); | ||
|
ematipico marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| server.listen(3000); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "name": "astro-test", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "packageManager": "pnpm@10.28.0" | ||
| } |
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.
[critical][correctness]: findUser mutates records due to assignment in predicateThe
users.findpredicate usesuser.id = Number(id)(assignment) instead ofuser.id === Number(id). On every lookup it overwrites the first user'sidwith the requested value and returns that mutated object if the assigned value is truthy. This corrupts the in-memory user store and makes/users/:idreturn the wrong record (orundefinedfor id0). Change the predicate to a strict comparison and consider using===consistently throughout the file.