London|25-SDC-NOV| FATMA DEGIRMENCI |Sprint 3|Middleware#69
Open
fatmaevin wants to merge 2 commits intoCodeYourFuture:mainfrom
Open
London|25-SDC-NOV| FATMA DEGIRMENCI |Sprint 3|Middleware#69fatmaevin wants to merge 2 commits intoCodeYourFuture:mainfrom
fatmaevin wants to merge 2 commits intoCodeYourFuture:mainfrom
Conversation
OracPrime
approved these changes
Feb 10, 2026
OracPrime
left a comment
There was a problem hiding this comment.
There is a subtle bug in terms of how you combine the chunks. Feel free to fix it as a stretch objective, but I think it's outside scope for this exercise. Otherwise good code.
| function bodyParser(req, res, next) { | ||
| let data = ""; | ||
| req.on("data", (chunk) => { | ||
| data += chunk; |
There was a problem hiding this comment.
Because the data is coming in as bytes, there is a risk here. data+=chunk will convert the chunk to string before adding it to data. But some characters in UTF8 can be up to 4 bytes long. If the chunk ends in the middle of such a character, this string conversion will go wrong. Safer is to concatenate the chunks as bytes and then convert to string at the end. Or cheat slightly and use the express text middleware to get the body as a string in one go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Learners, PR Template
Self checklist
Changelist
Exercise 1: Custom middleware
usernameMiddlewareto parse X-Username header.bodyParsermiddleware to manually parse POST JSON body as an array of strings./subjectsresponds with authentication info and list of subjects.Exercise 2: Built-in middleware
express.json()middleware.bodyMiddlewareto ensure POST body is an array of strings./subjectsbehavior remains the same.