-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /node_modules | ||
| package-lock.json | ||
| 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; | ||
|
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. Are you intentionally using || rather than ?? here? If yes, and you understand the difference, that's fine. |
||
| 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')); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| 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 | ||
|
|
||
| app.use(express.text({ type: 'application/json'})); | ||
|
||
|
|
||
| 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')); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "dependencies": { | ||
| "express": "^5.2.1" | ||
| } | ||
| } |
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.
No, you shouldn't ignore package-lock.json. Checking this file into version control is what allows another developer (or you, later) to replicate your build.