Skip to content

Commit b553fd4

Browse files
updates
1 parent d7cf339 commit b553fd4

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

  • off-the-shell-middleware

off-the-shell-middleware/app.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
11
import express from "express";
2+
import usernameMiddleware from "./middleware/usernameMiddleware.js";
23

34
const app = express();
45

5-
66
// built-in middleware
77
app.use(express.json());
8+
9+
app.post("/", usernameMiddleware, (req, res) => {
10+
const subjects = req.body;
11+
12+
if (!Array.isArray(subjects)) {
13+
return res.status(400).send("Body must be a JSON array");
14+
}
15+
16+
if (!subjects.every(item => typeof item === "string")) {
17+
return res.status(400).send("Array must contain only strings");
18+
}
19+
20+
const auth =
21+
req.username
22+
? `You are authenticated as ${req.username}.`
23+
: "You are not authenticated.";
24+
25+
let message = "";
26+
27+
if (subjects.length === 0) {
28+
message = "You have requested information about 0 subjects.";
29+
} else if (subjects.length === 1) {
30+
message = `You have requested information about 1 subject: ${subjects[0]}.`;
31+
} else {
32+
message = `You have requested information about ${subjects.length} subjects: ${subjects.join(", ")}.`;
33+
}
34+
35+
res.send(`${auth}\n\n${message}`);
36+
});
37+
38+
39+
app.listen(4001, () => {
40+
console.log("Server running on 4001");
41+
});

0 commit comments

Comments
 (0)