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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.env
54 changes: 54 additions & 0 deletions app-builtin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const express = require('express');
const app = express();
app.use(express.json());

// Header Middleware
const checkUsername = (req, res, next) => {
const usernameHeader = req.get("X-Username");
req.username = usernameHeader || null;
next();
};


// validates data
const validateSubjects = (req, res, next) => {

const subjects = req.body;

if (
!Array.isArray(subjects) ||
!subjects.every((item) => typeof item === "string")
) {
return res.status(400).send("Invalid input: expected an array of strings.");
}
next();
};



// The Endpoint
app.post("/subjects", checkUsername, validateSubjects, (req, res) => {
const { username, body: subjects } = req;

// Handle Authentication Message
const authPart = username
? `You are authenticated as ${username}.`
: "You are not authenticated.";

// Handle subject vs subjects
const count = subjects.length;
const subjectText = count === 1 ? "subject" : "subjects";

// 3. Handle the list formatting
const listPart = count > 0 ? `: ${subjects.join(", ")}.` : ".";

res.send(
`${authPart}\n\nYou have requested information about ${count} ${subjectText}${listPart}`,
);
});



app.listen(3000, () => {
console.log('Server is running on port 3000');
});
68 changes: 68 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const express = require('express');
const app = express();

// 1. Header Middleware
const checkUsername = (req, res, next) => {
const usernameHeader = req.get("X-Username");
req.username = usernameHeader || null;
next();
};

// 2. Body Parser Middleware
const ensureJsonStringArray = (req, res, next) => {
let rawBody = "";

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

req.on("end", () => {
let parsed;

try {
parsed = JSON.parse(rawBody);
} catch {
return res.status(400).send("Request body must be valid JSON");
}

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

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

req.body = parsed;
next();
});
};



// 3. The Endpoint
app.post("/subjects", checkUsername, ensureJsonStringArray, (req, res) => {
const { username, body: subjects } = req;

// Handle Authentication Message
const authPart = username
? `You are authenticated as ${username}.`
: "You are not authenticated.";

// Handle subject vs subjects
const count = subjects.length;
const subjectText = count === 1 ? "subject" : "subjects";

// 3. Handle the list formatting
const listPart = count > 0 ? `: ${subjects.join(", ")}.` : ".";

res.send(
`${authPart}\n\nYou have requested information about ${count} ${subjectText}${listPart}`,
);
});



app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Loading