generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 29
Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 3 | Middleware #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HassanOHOsman
wants to merge
17
commits into
CodeYourFuture:main
Choose a base branch
from
HassanOHOsman:feature/middlewares
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61,927
−0
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
92d0e54
install express on root directory
HassanOHOsman c88f10b
create a basic express server
HassanOHOsman 66c5e16
create middleware that looks for username header
HassanOHOsman 06eb95e
adding next() at the end of the 1st middleware so after the checks th…
HassanOHOsman ad65f57
Tell express to use the username middleware
HassanOHOsman b0f4852
update the express post route
HassanOHOsman c99ceef
correcting logic inside express post route so that response is sent b…
HassanOHOsman de3dacc
build middleware to parse post body into json array
HassanOHOsman c13ff77
make express use the json array middleware
HassanOHOsman 9f9f2e7
update the express post route to handle 2nd middleware logic
HassanOHOsman 5e1aa6e
reducing logic inside express post route so that the 2 middlewares ar…
HassanOHOsman 8dc2dcc
resuing shared codebase from custom middleware version, remove part a…
HassanOHOsman f6b9b6e
Add the built-in JSON parser middleware
HassanOHOsman 8c91401
adding a comment besides off-the-shelf Json parser/middleware
HassanOHOsman 25b8cfd
create .gitignore file in root dir and add node_modules
HassanOHOsman bca14f5
move body-parsing middleware so that instead of running globally with…
HassanOHOsman c0667af
add the other half of the response
HassanOHOsman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Middleware 1 looks for a header with name "X-Username" | ||
| function usernameMiddleware(request, response, next) { | ||
| const usernameHeader = request.header("X-Username"); | ||
| request.username = usernameHeader ? usernameHeader : null; | ||
| next(); | ||
| } | ||
|
|
||
| // Middleware 2 parse the POST body as JSON array | ||
| function postBodyAsJsonMiddleware(request, response, next) { | ||
| let data = ''; | ||
|
|
||
| request.on('data', chunk => { | ||
| data += chunk; | ||
| }); | ||
|
|
||
| request.on('end', () => { | ||
| try { | ||
| const parsedData = JSON.parse(data); | ||
|
|
||
| if (!Array.isArray(parsedData) || !parsedData.every(item => typeof item === 'string')) { | ||
| return response.status(400).send('Invalid request: must be a JSON array of strings') | ||
| } | ||
|
|
||
| request.body = parsedData; | ||
| next(); | ||
| } catch (err) { | ||
| response.status(400).send("Invalid JSON"); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| const express = require("express"); | ||
|
|
||
| const app = express(); | ||
| const PORT = 3000; | ||
|
|
||
| app.use(usernameMiddleware); | ||
|
|
||
|
|
||
|
|
||
| app.post("/", postBodyAsJsonMiddleware, (request, response) => { | ||
| let responseToClient1; | ||
| if (request.username) { | ||
| responseToClient1 = `You are authenticated as ${request.username}.`; | ||
| } else { | ||
| responseToClient1 = "You are not authenticated."; | ||
| } | ||
|
|
||
| const subjects = request.body || []; | ||
| let responseToClient2; | ||
|
|
||
| if (subjects.length === 0) { | ||
| responseToClient2 = "You have requested information about 0 subjects."; | ||
| } else { | ||
| responseToClient2 = `You have requested information about ${subjects.length} subject${subjects.length > 1 ? 's' : ''}: ${subjects.join(', ')}.`; | ||
| } | ||
|
|
||
| response.send(`${responseToClient1}\n${responseToClient2}`); | ||
| }); | ||
|
|
||
| app.listen(PORT, () => { | ||
| console.log(`Server running on http://localhost:${PORT}`); | ||
| }); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need a .gitignore file: you really don't want to be checking node_modules into git
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was meaning to ignore it, silly me! totally forgot. Anyway, it's been ignored now. Thank you :)