Repository navigation
Manchester | 26-Jun-SDC | Fithi Teklom | Sprint 3 | Middleware exercise #127
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 all 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 @@ | ||
| node_modules/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| const express = require("express"); | ||
|
|
||
| const app = express(); | ||
|
|
||
| const usernameMiddleware = (req, res, next) => { | ||
| const username = req.get("X-Username"); | ||
|
|
||
| if (username) { | ||
| req.username = username; | ||
| } else { | ||
| req.username = null; | ||
| } | ||
|
|
||
| next(); | ||
| }; | ||
|
|
||
| app.use(express.json()); | ||
|
|
||
| const validateSubjects = (req, res, next) => { | ||
| if (!Array.isArray(req.body) || !req.body.every(item => typeof item === "string")) { | ||
| return res | ||
| .status(400) | ||
| .send("Request body must be a JSON array of strings."); | ||
| } | ||
|
|
||
|
Comment on lines
+21
to
+25
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. Some good additional reading would be on error handling middleware this is an express middleware which specifically handles error - and we can use it to specifically point Express to a specific function when a error occurs, skipping anything else. |
||
| next(); | ||
| }; | ||
|
|
||
|
|
||
| app.post("/", usernameMiddleware, validateSubjects, (req, res) => { | ||
| const username = req.username; | ||
| const subjects = req.body; | ||
| const subjectCount = subjects.length; | ||
|
|
||
| const authenticationMessage = username | ||
| ? `You are authenticated as ${username}.` | ||
| : "You are not authenticated."; | ||
|
|
||
| let subjectsMessage; | ||
|
|
||
| if (subjectCount === 0) { | ||
| subjectsMessage = "You have requested information about 0 subjects."; | ||
| } else { | ||
| const subjectWord = subjectCount === 1 ? "subject" : "subjects"; | ||
|
|
||
| subjectsMessage = `You have requested information about ${subjectCount} ${subjectWord}: ${subjects.join(", ")}.`; | ||
| } | ||
|
|
||
| res.send(`${authenticationMessage}\n\n${subjectsMessage}\n`); | ||
| }); | ||
|
|
||
|
|
||
| app.listen(3001, () => { | ||
| console.log("Server listening on port 3001"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| const express = require("express"); | ||
|
|
||
| const app = express(); | ||
|
|
||
| const usernameMiddleware = (req, res, next) => { | ||
| const username = req.get("X-Username"); | ||
|
|
||
| if (username) { | ||
| req.username = username; | ||
| } else { | ||
| req.username = null; | ||
| } | ||
|
|
||
| next(); | ||
| }; | ||
|
|
||
| const bodyMiddleware = (req, res, next) => { | ||
| const chunks = []; | ||
|
|
||
| req.on("data", (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| req.on("end", () => { | ||
| try { | ||
| const body = JSON.parse(Buffer.concat(chunks).toString("utf8")); | ||
|
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. What does the Buffer.concat.toString argument do?
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.
|
||
|
|
||
| if (!Array.isArray(body) || !body.every(item => typeof item === "string")) { | ||
| return res | ||
| .status(400) | ||
| .send("Request body must be a JSON array of strings."); | ||
| } | ||
|
|
||
| req.body = body; | ||
| next(); | ||
| } catch { | ||
| res.status(400).send("Request body must be valid JSON."); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
| app.post("/", usernameMiddleware, bodyMiddleware, (req, res) => { | ||
| const username = req.username; | ||
| const subjects = req.body; | ||
| const subjectCount = subjects.length; | ||
|
|
||
| const authenticationMessage = username | ||
| ? `You are authenticated as ${username}.` | ||
| : "You are not authenticated."; | ||
|
|
||
| let subjectsMessage; | ||
|
|
||
| if (subjectCount === 0) { | ||
| subjectsMessage = "You have requested information about 0 subjects."; | ||
| } else { | ||
| const subjectWord = subjectCount === 1 ? "subject" : "subjects"; | ||
|
|
||
| subjectsMessage = `You have requested information about ${subjectCount} ${subjectWord}: ${subjects.join(", ")}.`; | ||
| } | ||
|
|
||
| res.send(`${authenticationMessage}\n\n${subjectsMessage}\n`); | ||
| }); | ||
|
|
||
| app.listen(3001, () => { | ||
| console.log("Server listening on port 3001"); | ||
| }); | ||
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.
We're looking for responses that look a specific way - specified in the coursework prep
Please amend your responses to reflect these instructions