Skip to content

Conversation

@fatmaevin
Copy link

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

Exercise 1: Custom middleware

  • Implemented usernameMiddleware to parse X-Username header.
  • Implemented bodyParser middleware to manually parse POST JSON body as an array of strings.
  • Endpoint /subjects responds with authentication info and list of subjects.
  • Validates input and rejects invalid JSON or non-string arrays.

Exercise 2: Built-in middleware

  • Removed custom bodyParser.
  • Added Express built-in express.json() middleware.
  • Added bodyMiddleware to ensure POST body is an array of strings.
  • Endpoint /subjects behavior remains the same.

@fatmaevin fatmaevin added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 9, 2026
@OracPrime OracPrime added the Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. label Feb 10, 2026
Copy link

@OracPrime OracPrime left a 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 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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@OracPrime OracPrime added Reviewed Volunteer to add when completing a review with trainee action still to take. Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Reviewed Volunteer to add when completing a review with trainee action still to take.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants