Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
66 changes: 66 additions & 0 deletions custom-middlewares/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Middleware 1 looks for a header with name "X-Username"

Choose a reason for hiding this comment

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

You need a .gitignore file: you really don't want to be checking node_modules into git

Copy link
Author

Choose a reason for hiding this comment

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

I was meaning to ignore it, silly me! totally forgot. Anyway, it's been ignored now. Thank you :)

function usernameMiddleware(request, response, next) {
const usernameHeader = request.header("X-Username");
request.username = usernameHeader ? usernameHeader : null;
next();
}

// Middleware 2 parse the POST body as JSON array
function postBodyAsJsonMiddleware(request, response, next) {
let data = '';

request.on('data', chunk => {
data += chunk;
});

request.on('end', () => {
try {
const parsedData = JSON.parse(data);

if (!Array.isArray(parsedData) || !parsedData.every(item => typeof item === 'string')) {
return response.status(400).send('Invalid request: must be a JSON array of strings')
}

request.body = parsedData;
next();
} catch (err) {
response.status(400).send("Invalid JSON");
}
});
}



const express = require("express");

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

app.use(usernameMiddleware);



app.post("/", postBodyAsJsonMiddleware, (request, response) => {
let responseToClient1;
if (request.username) {
responseToClient1 = `You are authenticated as ${request.username}.`;
} else {
responseToClient1 = "You are not authenticated.";
}

const subjects = request.body || [];
let responseToClient2;

if (subjects.length === 0) {
responseToClient2 = "You have requested information about 0 subjects.";
} else {
responseToClient2 = `You have requested information about ${subjects.length} subject${subjects.length > 1 ? 's' : ''}: ${subjects.join(', ')}.`;
}

response.send(`${responseToClient1}\n${responseToClient2}`);
});

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

Loading