LONDON | 25-SDC-Nov | Zohreh Kazemianpour | Sprint 3 | Middleware#66
LONDON | 25-SDC-Nov | Zohreh Kazemianpour | Sprint 3 | Middleware#66zohrehKazemianpour wants to merge 3 commits into
Conversation
OracPrime
left a comment
There was a problem hiding this comment.
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.
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.
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.
|
Closing PR because the SDC run has finished. Feel free to re-open if you're still working on it. |
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.