Skip to content

Commit 4ef9838

Browse files
committed
Add README.md file
1 parent bd66013 commit 4ef9838

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

midlleware-Ex2/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Middleware Exercise 1 – Custom Express Middlewares
2+
3+
## Description
4+
5+
This project is a small Express application demonstrating how to create and use **custom middleware**.
6+
7+
The application includes:
8+
9+
* A middleware that reads the `X-Username` request header and attaches it to the request
10+
* A middleware that manually parses the POST request body as a JSON array of strings
11+
* A POST endpoint that uses the data provided by the middlewares to build a response
12+
13+
---
14+
15+
## Setup & Run
16+
17+
```bash
18+
npm install
19+
node server.js
20+
```
21+
22+
Server runs on:
23+
24+
```
25+
http://localhost:3000
26+
```
27+
28+
---
29+
30+
## POST Request
31+
32+
* Endpoint: `POST /`
33+
* Body must be a **JSON array of strings**
34+
* Header `X-Username` is optional
35+
36+
---
37+
38+
## Example Request
39+
40+
```bash
41+
curl -X POST \
42+
-H "X-Username: Ahmed" \
43+
--data '["Bees"]' \
44+
http://localhost:3000
45+
```
46+
47+
---
48+
49+
## Example Response
50+
51+
```
52+
You are authenticated as Ahmed.
53+
You have requested information about 1 subject: Bees.
54+
``
55+
---

midlleware-Ex2/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const app = express();
44
app.use(express.json()); //built in JSON parser
55
const PORT = 3000;
66

7+
78
function usernameMiddleware(req, res, next) {
89
const headerValue = req.header("X-Username"); // Express helper for headers
910
req.username = headerValue ? headerValue : null;

0 commit comments

Comments
 (0)