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
2 changes: 2 additions & 0 deletions chat-app/.gitignore

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.

Did you mean to commit this file here?

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.

first commit is for middleware files and directory inside middleware and "adding files directories" is for off the shell files and folders inside that

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.

OK. The middleware files look good to me. But can you remove the chat app files if these are not required for this specific task?

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.env
2 changes: 2 additions & 0 deletions middleware-costum/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.env
31 changes: 31 additions & 0 deletions middleware-costum/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import express from "express";
import usernameMiddleware from "./middleware/usernameMiddleware.js";
import jsonArrayMiddleware from "./middleware/jsonArrayMiddleware.js";

const app = express();

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

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

let message = "";

if (subjects.length === 0) {
message = "You have requested information about 0 subjects.";
} else if (subjects.length === 1) {
message = `You have requested information about 1 subject: ${subjects[0]}.`;
} else {
message = `You have requested information about ${subjects.length} subjects: ${subjects.join(", ")}.`;
}

res.send(`${auth}\n\n${message}`);
});


app.listen(4000, () => {
console.log("Server running on 4000");
});
31 changes: 31 additions & 0 deletions middleware-costum/middleware/jsonArrayMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default function jsonArrayMiddleware(req, res, next) {
const chunks = [];

req.on("data", (chunk) => {
chunks.push(chunk);
});

req.on("end", () => {
const bodyString = Buffer.concat(chunks).toString();

let data;

try {
data = JSON.parse(bodyString);
} catch {
return res.status(400).send("Invalid JSON");
}

if (!Array.isArray(data)) {
return res.status(400).send("Body must be an array");
}

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

req.body = data;

next();
});
}
7 changes: 7 additions & 0 deletions middleware-costum/middleware/usernameMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function usernameMiddleware(req, res, next) {
const username = req.header("X-Username");

req.username = username || null;

next();
}
Loading
Loading