-
-
Notifications
You must be signed in to change notification settings - Fork 25
LONDON | 25-SDC-Nov | Zohreh Kazemianpour | Sprint 3 | Middleware #66
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?
LONDON | 25-SDC-Nov | Zohreh Kazemianpour | Sprint 3 | Middleware #66
Conversation
OracPrime
left a comment
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.
One comment to understand.
One bug to at least understand. It's subtle enough you don't need to fix it unless you want a stretch task.
Otherwise solid code
| // Header Middleware | ||
| const checkUsername = (req, res, next) => { | ||
| const usernameHeader = req.get("X-Username"); | ||
| req.username = usernameHeader || null; |
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.
Are you deliberately using || instead of ?? here? Are you clear what the difference is? If yes to both, then no problem.
| let rawBody = ""; | ||
|
|
||
| req.on("data", (chunk) => { | ||
| rawBody += chunk; |
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.
There is a subtle bug here. chunk is bytes. When you do rawBody+=chunk it does a toString. However a single character in UTF8 can be up to 4 bytes. If your chunk ends in the middle of those 4 then the string conversion goes wrong. Better is to concatenate the byte chunks and convert once.
You must title your PR like this:
Region | Cohort | FirstName LastName | Sprint | Assignment Title
Learners, PR Template
Self checklist
Changelist
I finished the middleware exercise. I made two versions to show the difference:
app.js: I wrote my own parser using req.on('data') and req.on('end'). This helps me understand how Node.js actually receives data chunks.
app-builtin.js: I swapped my manual code for express.json().
I spent some time debugging the curl commands. I found out that:
My manual parser was accepted anything.
express.json() is more strict. It only works if I add the -H "Content-Type: application/json" header to the curl command.
Logic included
I also added code to check if the user is authenticated with X-Username and made sure the response handles plural words correctly.