Skip to content

Glasgow | 26 March SDC | Mohammed Abdoon| Sprint 3 | Middleware exercises#105

Open
M-Abdoon wants to merge 2 commits into
CodeYourFuture:mainfrom
M-Abdoon:middleware
Open

Glasgow | 26 March SDC | Mohammed Abdoon| Sprint 3 | Middleware exercises#105
M-Abdoon wants to merge 2 commits into
CodeYourFuture:mainfrom
M-Abdoon:middleware

Conversation

@M-Abdoon

@M-Abdoon M-Abdoon commented Jul 1, 2026

Copy link
Copy Markdown

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

@M-Abdoon M-Abdoon added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Jul 1, 2026
const status = this.user ? `Logged in: ${this.user}` : "Anonymous";
const count = this.entries.length;
const itemType = count === 1 ? "item" : "items";
const details = count > 0 ? `[${this.entries.map(e => `"${e}"`).join(", ")}]` : "[]";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What if an element of entries contains a double quote character?

Have you considered using a built-in JS function to simplify the encoding task?

Comment on lines +16 to +21
extractCredentials() {
this.user = this.req.get("X-Username");
return this;
}

processPayload() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These middleware functions/methods do not quite adhere to the Express middleware pattern.

Comment on lines +22 to +25
const raw = this.req.body;
if (!raw || !raw.trim()) {
return this;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the spec, the middleware is expected to "parse the request POST body" (instead of consuming the parsed req.body content).

Suggestion: Look up (ask AI)

How to implement an Express middleware to parse POST body without using Express body parsers?

const app = express();
const port = 3001;

app.use(express.text({ type: "application/x-www-form-urlencoded" }));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

One of your middleware is expected to parse the raw POST body. You should not need this.

Comment on lines +38 to +49
buildReply() {
const status = this.user ? `Logged in: ${this.user}` : "Anonymous";
const count = this.entries.length;
const itemType = count === 1 ? "item" : "items";
const details = count > 0 ? `[${this.entries.map(e => `"${e}"`).join(", ")}]` : "[]";

return `Status: ${status}\nQuery: ${count} ${itemType}\nData: ${details}`;
}

send() {
this.res.send(this.buildReply());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These two operations should not be tightly coupled with the middleware you implemented. Supposedly in the off-the-shelf-middleware, you could just replace the middleware functions and everything else works the same.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the spec, this app is expected to behave like the app implemented in "custom-middleware".

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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