Skip to content

Conversation

@delmorallopez
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

@delmorallopez delmorallopez added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jan 27, 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.

A couple of comments to understand but no changes required

*/
const usernameMiddleware = (req, res, next) => {
const username = req.header("X-Username");
req.username = username ? username : null;

Choose a reason for hiding this comment

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

This code works, but it would probably be more normal to see either

req.username = username ?? null;

or

req.username = username || null;

A useful exercise is to understand the difference between the two and why the first one 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.

I understand the defferent
Fisrt one is better option because:
Safer, more explicit, less error-prone, modern JavaScript best practice

Choose a reason for hiding this comment

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

It depends whether 0 and/or an empty string are valid usernames as well!

let rawBody = "";

req.on("data", chunk => {
rawBody += chunk;

Choose a reason for hiding this comment

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

This is good use of the req.on model, although other trainees just went for app.use(express.text()) !

Copy link
Author

Choose a reason for hiding this comment

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

I can see this option

  • Express middleware parses the request body automatically
  • Converts the body into a string and stores it in req.body
  • Less code, easier to use for trainees

Choose a reason for hiding this comment

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

You do have to be careful though - unless you use text with type="application/json" it will just ignore the body!

@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. 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