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

76 changes: 76 additions & 0 deletions Sprint 3- middlewares/custom-written middlewares/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const express = require("express");

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

// Middleware 1: get username from 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);

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

// Every item must be a string
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.");
}
});
}

// Use our two middlewares
app.use(usernameMiddleware);
app.use(bodyMiddleware);

// POST endpoint
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(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
52 changes: 52 additions & 0 deletions Sprint 3- middlewares/off-shelf middleware/off shelf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const express = require("express");

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

// Built-in Express JSON middleware
app.use(express.json());

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

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

next();
}

app.use(usernameMiddleware);

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

if (!Array.isArray(subjects) ||
!subjects.every((subject) => typeof subject === "string")) {
return res.status(400).send("Invalid request 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(", ") : ""}.`
);
}
Comment on lines +33 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not an error but I would like to suggest a better approach.

What is the output difference between if and else statements. Read those articles.

  1. Template literals
  2. Ternary operators

Hope you find a "clean code" way!

});

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