Birmingham | 26-SDC-Mar | Chioma Okeke | Sprint 3 | Middleware Exercises#98
Birmingham | 26-SDC-Mar | Chioma Okeke | Sprint 3 | Middleware Exercises#98JanefrancessC wants to merge 12 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
LonMcGregor
left a comment
There was a problem hiding this comment.
Good work on this task, I have a couple of comments however.
There was a problem hiding this comment.
Did you mean to commit this change to this file here?
There was a problem hiding this comment.
No, it was an error. That's why i deleted the file
There was a problem hiding this comment.
Can you fix it so that this file is no longer listed in the "changed files" for this pull request?
| // Authenticate if a username is provided | ||
| const authLine = req.username | ||
| ? `You're authenticated as ${req.username}` | ||
| : `You're not authenticated!`; |
There was a problem hiding this comment.
If the user is not authenticated, should the app be returning anything?
There was a problem hiding this comment.
For this exercise, if X-username isn't present, req.username is set to null. My understanding of it is that the middleware will recognise authenticated users but not enforce authentication.
There was a problem hiding this comment.
Typically if someone is not authorised or not authenticated we might want the app to behave a bit differently. Here is says not authenticated but still returns data. Is there any alternative approach you could take?
Learners, PR Template
Self checklist
Changelist
Added two Express server middleware implementations.
custom-middleware
Implements two custom Express middlewares:
extractUsernamereads theX-Usernameheader and attaches it toreq.username.parseJsonArrayBodymanually collects incoming request body chunks, parses them as JSON, and validates that the result is an array of strings.This version demonstrates low-level request handling without relying on Express built-in body parsing.
off-the-shelf-middleware
Replaces the custom body parsing middleware with
express.json().This version highlights a key difference in Express behaviour:
express.json()only processes requests when theContent-Type: application/jsonheader is present. As a result, earliercurlrequests using--datawithout explicitly setting the content type were not parsed correctly.