Exercises to practice and solidify your understanding of the Decomposition module of the Software Development Course.
This repo contains two small Express apps that both implement the same /subjects POST endpoint, but in different ways:
custom-middlewares/uses custom middleware for header extraction and JSON array parsing.off-the-shelf-middleware/uses Express built-in JSON parsing plus a custom validator.
Both apps respond with an authentication message based on the X-Username request header and validate that the request body is a JSON array of strings.
Location: custom-middlewares/
Middleware:
extractUsernamereadsX-Usernameand setsreq.username.parseJsonArrayBodymanually parses the JSON body and ensures it is an array of strings.
Run tests:
cd custom-middlewares
npm install
npm testLocation: off-the-shelf-middleware/
Middleware:
express.json()parses JSON bodies.extractUsernamereadsX-Usernameand setsreq.username.validateJsonArraychecks the parsed body is an array of strings.
Run tests:
cd off-the-shelf-middleware
npm install
npm testPOST /subjects
- Body must be a JSON array of strings.
- If
X-Usernameis present: responds withYou are authenticated as <name>. - If
X-Usernameis missing: responds withYou are not authenticated.