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
17 changes: 17 additions & 0 deletions sprint3_exercises/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Dependencies
node_modules/

# Environment
.env
.env.local

# Logs
*.log

# IDE
.vscode/
.idea/

# OS
.DS_Store

50 changes: 50 additions & 0 deletions sprint3_exercises/middleware-existing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import express from "express";

const app = express();
const PORT = 3000;

// Middleware 1: Extract username from X-Username header
function extractUsername(req, res, next) {
const username = req.headers["x-username"];
req.username = username || null;
next();
}

// Use built-in Express JSON middleware instead of custom one
app.use(extractUsername);
app.use(express.json());

app.post("/", (req, res) => {
// Validate that body is an array
if (!Array.isArray(req.body)) {
return res.status(400).send("Request body must be a JSON array");

Choose a reason for hiding this comment

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

Because you're using the express.json middleware, does that already check something is a JSON array body? Are you giving the correct response here?

Copy link
Author

Choose a reason for hiding this comment

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

express.json only parses and validates JSON syntax; it does not enforce that the body is an array. We still need separate validation for shape and element types.

Yes, the 400 responses for “must be a JSON array” and “all elements must be strings” are correct for validation failures after parsing

}

// Check if all elements are strings
const allStrings = req.body.every((item) => typeof item === "string");
if (!allStrings) {
return res.status(400).send("All array elements must be strings");
}

const authMessage = req.username
? `You are authenticated as ${req.username}.`
: "You are not authenticated.";

const count = req.body.length;
let subjectsMessage;
if (count === 0) {
subjectsMessage = "You have requested information about 0 subjects.";
} else if (count === 1) {
subjectsMessage = `You have requested information about 1 subject: ${req.body[0]}.`;
} else {
subjectsMessage = `You have requested information about ${count} subjects: ${req.body.join(
", "
)}.`;
}

res.send(`${authMessage}\n\n${subjectsMessage}\n`);
});

app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
56 changes: 56 additions & 0 deletions sprint3_exercises/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import express from "express";

const app = express();
const PORT = 3000;

// Middleware 1: Extract username from X-Username header
function extractUsername(req, res, next) {
const username = req.headers["x-username"];
req.username = username || null;
next();
}

// Middleware 2: Parse POST body as JSON (reusable for any JSON parsing)
app.use(express.json());

// Middleware 3: Validate that body is an array of strings
function validateArrayOfStrings(req, res, next) {
// Check if it's an array
if (!Array.isArray(req.body)) {
return res.status(400).send("Request body must be a JSON array");
}

// Check if all elements are strings
const allStrings = req.body.every((item) => typeof item === "string");
if (!allStrings) {
return res.status(400).send("All array elements must be strings");
}

next();
}

// Apply middlewares
app.use(extractUsername);
app.post("/", validateArrayOfStrings, (req, res) => {
const authMessage = req.username
? `You are authenticated as ${req.username}.`
: "You are not authenticated.";

const count = req.body.length;
let subjectsMessage;
if (count === 0) {
subjectsMessage = "You have requested information about 0 subjects.";
} else if (count === 1) {
subjectsMessage = `You have requested information about 1 subject: ${req.body[0]}.`;
} else {
subjectsMessage = `You have requested information about ${count} subjects: ${req.body.join(
", "
)}.`;
}

res.send(`${authMessage}\n\n${subjectsMessage}\n`);
});

app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
Loading