-
-
Notifications
You must be signed in to change notification settings - Fork 33
WestMidlands | SDC-NOV-2025 | Sara Tahir| Sprint 3| Middleware #65
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 2 commits
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,47 @@ | ||
| /* | ||
| Removed custom middleware that manually parsed raw text bodies | ||
| Enabled Express’s built‑in JSON parser | ||
| Updated validation middleware to work with parsed JSON | ||
| Fixed curl command by switching Content-Type | ||
| to application/json (using: curl -X POST http://localhost:3000/subjects | ||
| -H "Content-Type: application/json" | ||
| -H "X-Username: Ahmed" | ||
| --data '["Bees"]') | ||
| */ | ||
| import express from "express"; | ||
| const app = express(); | ||
| app.use(express.json()); // express builtin JSON parser | ||
|
|
||
|
|
||
| // Middleware - Extracting username | ||
|
|
||
| function usernameMiddleware(req, res, next){ //middleware always has three parameters. req from client, res you send back and a next function that passes controls to next middleware | ||
|
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. Sort of. If you know you are the last middleware in the chain ("endware" I guess) then you don't need the next parameter - indeed the last (res,req)=> on a route declaration is just an example of this endware pattern. And there's also a special case middleware for error handling that has four parameters.
Author
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. Thanks for the clarification. |
||
| const userName = req.header("X-Username"); //getting a custom HTTP Header from the request | ||
| req.userName = userName || null; //if username is not presnet store null | ||
| next(); // this passes control to the next middleware. | ||
| } | ||
|
|
||
|
|
||
| app.post("/subjects",usernameMiddleware, (req, res) => { | ||
| const userName = req.userName; | ||
| const subjects = req.body; | ||
| const count = subjects.length; | ||
| const authMessage = userName ? `You are authenticated as ${userName}.` : `You are not authenticated.`; | ||
| // Subject message | ||
| let subjectMessage; | ||
| if (count === 0) { | ||
| subjectMessage = "You have requested information about 0 subjects."; | ||
| } else if (count === 1) { | ||
| subjectMessage = `You have requested information about 1 subject: ${subjects[0]}.`; | ||
| } else { | ||
| subjectMessage = `You have requested information about ${count} subjects: ${subjects.join(", ")}.`; | ||
| } | ||
|
|
||
| // Send final response | ||
| res.send(`${authMessage}\n\n${subjectMessage}`); | ||
| }); | ||
|
|
||
| const PORT = 3000 | ||
| app.listen(PORT, () => { | ||
| console.log(`Server running on http://localhost:${PORT}`) | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import express from "express"; | ||
| const app = express(); | ||
| app.use(express.text()); | ||
|
|
||
| //1st Middleware - Extracting username | ||
|
|
||
| function usernameMiddleware(req, res, next){ //middleware always has three parameters. req from client, res you send back and a next function that passes controls to next middleware | ||
| const userName = req.header("X-Username"); //getting a custom HTTP Header from the request | ||
| req.userName = userName || null; //if username is not presnet store null | ||
| next(); // this passes control to the next middleware. | ||
| } | ||
|
|
||
| //2nd Middleware - Validating JSON array body | ||
| function jsonArrayMiddleware(req, res, next){ | ||
| let data; | ||
| try { | ||
| data = JSON.parse(req.body); | ||
| } | ||
| catch(err){ | ||
| return res.status(400).send("Body must be valid JSON"); | ||
| } | ||
| if(!Array.isArray(data)){ | ||
| return res.status(400).send("Body must be JSON array"); | ||
|
|
||
| } | ||
| if (!data.every(item => typeof item === "string")) { //every item in the array is a string = NOT TRUE | ||
| return res.status(400).send("Array must contain only strings"); | ||
| } | ||
| req.body = data; //store validated array | ||
| next() //allows req to continue to next middleware | ||
| } | ||
|
|
||
| // POST route using both middlewares | ||
| app.post("/subjects",usernameMiddleware, jsonArrayMiddleware, (req, res) => { | ||
| const userName = req.userName; | ||
| const subjects = req.body; | ||
| const count = subjects.length; | ||
| const authMessage = userName ? `You are authenticated as ${userName}.` : `You are not authenticated.`; | ||
| // Subject message | ||
| let subjectMessage; | ||
| if (count === 0) { | ||
| subjectMessage = "You have requested information about 0 subjects."; | ||
| } else if (count === 1) { | ||
| subjectMessage = `You have requested information about 1 subject: ${subjects[0]}.`; | ||
| } else { | ||
| subjectMessage = `You have requested information about ${count} subjects: ${subjects.join(", ")}.`; | ||
| } | ||
|
|
||
| // Send final response | ||
| res.send(`${authMessage}\n\n${subjectMessage}`); | ||
| }); | ||
|
|
||
| const PORT = 3000 | ||
| app.listen(PORT, () => { | ||
| console.log(`Server running on http://localhost:${PORT}`) | ||
| }); |
| 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.
You should always check in package-lock into version control. This is what allows someone else to replicate your build. node_modules is definitely an ignore though!
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.
Done.