Skip to content
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
72 changes: 72 additions & 0 deletions middleware-exercises/Custom-Middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const express = require("express");

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

// Middleware 1:check X-Username header
function usernameMiddleware(req, res, next) {
const username = req.get("X-Username");

if (username) {
req.username = username;
} else {
req.username = null;
}

next();
}

// Middleware 2: parse and validate JSON array
function bodyMiddleware(req, res, next) {
let body = "";

req.on("data", (chunk) => {
body += chunk;
});

req.on("end", () => {
try {
const parsedBody = JSON.parse(body);

if (!Array.isArray(parsedBody)) {
return res.status(400).send("Request body must be a JSON array.");
}

if (!parsedBody.every((item) => typeof item === "string")) {
return res.status(400).send("Array must contain only strings.");
}

req.body = parsedBody;
next();
} catch (error) {
res.status(400).send("Request body must be valid JSON.");
}
});
}

app.use(usernameMiddleware);
app.use(bodyMiddleware);

app.post("/", (req, res) => {
const subjects = req.body;

if (req.username) {
res.send(
`You are authenticated as ${req.username}.\n\n` +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Given these messages are quite similar, could you rework the if block to reduce the code duplication?

`You have requested information about ${subjects.length} ` +
`${subjects.length === 1 ? "subject" : "subjects"}` +
`${subjects.length > 0 ? ": " + subjects.join(", ") : ""}.`,
);
} else {
res.send(
`You are not authenticated.\n\n` +
`You have requested information about ${subjects.length} ` +
`${subjects.length === 1 ? "subject" : "subjects"}` +
`${subjects.length > 0 ? ": " + subjects.join(", ") : ""}.`,
);
}
});

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

const app = express();

function usernameMiddleware(req, res, next) {
const username = req.get("X-Username");

if (username) {
req.username = username;
} else {
req.username = null;
}

next();
}

app.use(express.json());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does express.json() do all of the checks you did in your original code?

app.use(usernameMiddleware);

app.post("/", (req, res) => {
const subjects = req.body;

if (req.username) {
res.send(
`You are authenticated as ${req.username}.\n\n` +
`You have requested information about ${subjects.length} ` +
`${subjects.length === 1 ? "subject" : "subjects"}` +
`${subjects.length > 0 ? `: ${subjects.join(", ")}` : ""}.`,
);
} else {
res.send(
`You are not authenticated.\n\n` +
`You have requested information about ${subjects.length} ` +
`${subjects.length === 1 ? "subject" : "subjects"}` +
`${subjects.length > 0 ? `: ${subjects.join(", ")}` : ""}.`,
);
}
});

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