Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
55 changes: 55 additions & 0 deletions middleware-built-in/server.js

Copy link
Copy Markdown

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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");
});
67 changes: 67 additions & 0 deletions middleware-custom/server.js
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"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the Buffer.concat.toString argument do?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer.concat(chunks) combines all the chunks of the incoming request body into one Buffer. .toString("utf8") converts the bytes in that Buffer into a string using UTF-8 encoding. We then pass that string to JSON.parse() to convert the JSON text into a JavaScript value.


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");
});
Loading
Loading