Skip to content

Sheffield | 25-SDC-Nov | Sheida Shabankari | Sprint 3 |Middleware exercises#64

Open
sheida-shab wants to merge 1 commit intoCodeYourFuture:mainfrom
sheida-shab:middleware-exercises
Open

Sheffield | 25-SDC-Nov | Sheida Shabankari | Sprint 3 |Middleware exercises#64
sheida-shab wants to merge 1 commit intoCodeYourFuture:mainfrom
sheida-shab:middleware-exercises

Conversation

@sheida-shab
Copy link

  • 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

Description:
This PR adds two directories for the middleware exercises:

middleware-custom/ – Express app with two custom middlewares: reading X-Username header and parsing/validating JSON array POST body.

middleware-express-json/ – Same app using Express’s built-in express.json() middleware instead of custom body parsing.

Both apps have a POST endpoint returning authentication status and requested subjects.

@sheida-shab sheida-shab added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Decomposition The name of the module. labels Jan 30, 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.

Two comments to understand. Don't bother fixing the bug unless you fancy the stretch objective. Otherwise solid code.

//Middleware #۱:
app.use((req,res,next)=>{
const usernameHeader=req.headers["x-username"];
req.username=usernameHeader ? usernameHeader : null;

Choose a reason for hiding this comment

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

This works, but would more often be seen as

req.username = usernameHeader ?? null;

or

req.username = usernameHeader || null;

An interesting exercise is to understand the difference between these two and why the first is better.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the feedback! That was a really useful point — understanding the difference between || and ?? helped me learn a practical new detail about handling defaults more safely

app.use((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.

There is a subtle bug here. chunk is bytes, not a string. data+=chunk will do a convert to string. In UTF8 a single character can be as many as 4 bytes (even though most are 1). If the chunk happens to end in the middle of a long character, the conversion to string will go wrong. You're saving concatenating all the bytes first and then converting to string afterwards. It's probably out of scope for this exercise though.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for pointing that out — I wasn’t aware of the UTF-8 edge case with chunks. That’s a really useful detail to learn, even if it’s out of scope for this exercise.

@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 Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. 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. Module-Decomposition The name of the module. 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