Skip to content

Commit 53374b3

Browse files
committed
Add middleware-custom package with basic message handling API
- Created package.json for middleware-custom with express dependency - Implemented server.js to handle health check and message CRUD operations - Added middleware for request logging and API key validation
1 parent f7ed68f commit 53374b3

8 files changed

Lines changed: 971 additions & 0 deletions

File tree

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Node
2+
node_modules/
3+
4+
# Env files
5+
.env
6+
.env.*
7+
8+
# Logs
9+
logs/
10+
*.log
11+
npm-debug.log*
12+
13+
# Runtime data
14+
pids/
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Coverage
20+
coverage/
21+
.nyc_output/
22+
23+
# Build output
24+
dist/
25+
build/
26+
27+
# OS/editor
28+
.DS_Store
29+
.vscode/

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
# Coursework
22

33
Exercises to practice and solidify your understanding of the Decomposition module of the Software Development Course.
4+
5+
## Middleware exercises
6+
7+
This repo includes two Express examples for the Sprint 3 middleware prep:
8+
9+
- Custom middlewares: [middleware-custom/](middleware-custom)
10+
- Off-the-shelf middleware: [middleware-off-the-shelf/](middleware-off-the-shelf)
11+
12+
Each folder has its own README with run instructions.

middleware-custom/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Custom middleware example
2+
3+
This Express app uses two custom middlewares:
4+
- `requestLogger` logs each request with status and duration.
5+
- `requireApiKey` protects `POST /messages` using an API key header.
6+
7+
## Run locally
8+
9+
```bash
10+
npm install
11+
npm start
12+
```
13+
14+
Set a custom API key (optional):
15+
16+
```bash
17+
API_KEY=my-secret npm start
18+
```
19+
20+
## Try it
21+
22+
```bash
23+
curl http://localhost:3000/health
24+
curl http://localhost:3000/messages
25+
curl -X POST http://localhost:3000/messages \
26+
-H "Content-Type: application/json" \
27+
-H "x-api-key: cyf" \
28+
-d '{"user":"Azin","message":"Hello"}'
29+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function requestLogger(req, res, next) {
2+
const startedAt = Date.now();
3+
4+
res.on("finish", () => {
5+
const durationMs = Date.now() - startedAt;
6+
console.log(`${req.method} ${req.originalUrl} ${res.statusCode} ${durationMs}ms`);
7+
});
8+
9+
next();
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function requireApiKey(req, res, next) {
2+
const expectedKey = process.env.API_KEY || "cyf";
3+
const providedKey = req.header("x-api-key");
4+
5+
if (providedKey !== expectedKey) {
6+
return res.status(401).json({ error: "Invalid API key" });
7+
}
8+
9+
next();
10+
};

0 commit comments

Comments
 (0)