generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 26
London | 25-SDC-Nov | Emiliano Uruena | Sprint 3 | Decomposition Middleware #63
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
Emilianouz
wants to merge
2
commits into
CodeYourFuture:main
Choose a base branch
from
Emilianouz:sprint3
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.
+925
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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,52 @@ | ||
| const express = require('express'); | ||
| const app = express(); | ||
|
|
||
| // middleware for x-user header | ||
| const usernameMiddleware = (req,res,next) => { | ||
| const usernameHeader = req.get('X-Username'); | ||
| // if exists, set if, otherwise set to null | ||
| req.username = usernameHeader || null; | ||
| next(); | ||
| }; | ||
|
|
||
| // middleware for body parsing and validation | ||
|
|
||
| app.use(express.text({ type: 'application/json'})); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! adding that type parameter is necessary for this use case but easy to forget |
||
|
|
||
| const validateArrayMiddleware = (req, res, next) => { | ||
|
|
||
| try { | ||
| // attempt to parse the raw text body | ||
| const data = JSON.parse(req.body); | ||
|
|
||
| //check if it is an array AND every element is a string | ||
| const isStringArray = Array.isArray(data) && data.every(item => typeof item === 'string'); | ||
| if (!isStringArray){ | ||
| return res.status(400).send("invalid input: body must be JSON array of strings"); | ||
| } | ||
|
|
||
| // attach the parsed array to req.body for next handler | ||
| req.body = data; | ||
| next(); | ||
| } catch (error){ | ||
| return res.status(400).send("invalid JSON format"); | ||
| } | ||
| }; | ||
| // Applying the middlewares to the POST endpoint | ||
|
|
||
| app.post('/subjects', usernameMiddleware, validateArrayMiddleware, (req,res) => { | ||
| const userText = req.username | ||
| ? `You are authenticated as ${req.username}.` | ||
| : "Your are not authenticated."; | ||
|
|
||
| const subjectCount = req.body.length; | ||
| const subjectList = req.body.join(', '); | ||
| const subjectSuffix = subjectCount === 1 ? 'subject' : 'subjects'; | ||
|
|
||
| const subjectsText = `You have requested information about ${subjectCount} ${subjectSuffix} ${subjectCount > 0 ? ': ' + subjectList : ''}.`; | ||
|
|
||
| res.send(`${userText}\n\n${subjectsText}`); | ||
| } | ||
| ); | ||
|
|
||
| app.listen(3000, () => console.log('server running on http://localhost:3000')); | ||
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,44 @@ | ||
| const express = require('express'); | ||
| const app = express(); | ||
|
|
||
| app.use(express.json()); | ||
|
|
||
| // middleware for x-user header | ||
| const usernameMiddleware = (req,res,next) => { | ||
| const usernameHeader = req.get('X-Username'); | ||
| // if exists, set if, otherwise set to null | ||
| req.username = usernameHeader || null; | ||
| next(); | ||
| }; | ||
|
|
||
| // middleware for body parsing and validation | ||
|
|
||
| const validateArrayMiddleware = (req, res, next) => { | ||
| // express.json() did the parsing, so.. | ||
| const data = req.body; | ||
|
|
||
| //check if it is an array AND every element is a string | ||
| const isStringArray = Array.isArray(data) && data.every(item => typeof item === 'string'); | ||
| if (!isStringArray){ | ||
| return res.status(400).send("invalid input: body must be JSON array of strings"); | ||
| } | ||
| next(); | ||
| }; | ||
| // Applying the middlewares to the POST endpoint | ||
|
|
||
| app.post('/subjects', usernameMiddleware, validateArrayMiddleware, (req,res) => { | ||
| const userText = req.username | ||
| ? `You are authenticated as ${req.username}.` | ||
| : "Your are not authenticated."; | ||
|
|
||
| const subjectCount = req.body.length; | ||
| const subjectList = req.body.join(', '); | ||
| const subjectSuffix = subjectCount === 1 ? 'subject' : 'subjects'; | ||
|
|
||
| const subjectsText = `You have requested information about ${subjectCount} ${subjectSuffix} ${subjectCount > 0 ? ': ' + subjectList : ''}.`; | ||
|
|
||
| res.send(`${userText}\n\n${subjectsText}`); | ||
| } | ||
| ); | ||
|
|
||
| app.listen(3000, () => console.log('server running on http://localhost:3000')); |
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.
Are you intentionally using || rather than ?? here? If yes, and you understand the difference, that's fine.