Skip to content

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.

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.

@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