dj-express
v1.0.2
Published
A minimal Express.js implementation for beginners - learn how Express works internally
Maintainers
Readme
DJ Express
A minimal Express.js implementation for beginners to understand how Express works internally.
Perfect for learning: See how routing, middleware, and request handling work under the hood!
Why DJ Express?
- 🎓 Educational: Learn Express internals in ~100 lines of code
- 🔍 Transparent: See exactly how
app.get(),next(), andres.send()work - 🚀 Simple: No complex abstractions, just pure Node.js
- 📦 Lightweight: Zero dependencies
Installation
npm i dj-expressQuick Start
Option 1: Use as npm package
const { createApp } = require("dj-express");
const app = createApp();
app.get("/", (req, res) => {
res.json({ message: "Hello from dj-express!" });
});
app.listen(3000);Option 2: Run the example
git clone <repository-url>
cd mini-express
node example.jsHow Express Works (Internally)
1. Routes are just an Array
const routes = [];
app.get = (path, handler) => {
routes.push({ method: "GET", path, handler });
};When you call app.get("/users", handler), it simply adds to an array!
2. Middlewares are also an Array
const middlewares = [];
app.use = (middleware) => {
middlewares.push(middleware);
};3. The next() Function (Magic!)
let i = 0;
function next() {
const middleware = middlewares[i++];
if (middleware) {
middleware(req, res, next);
} else {
handleRoute();
}
}next() simply calls the next function in the array!
4. res.send() is just a wrapper
res.send = (data) => {
if (typeof data === "object") {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(data));
} else {
res.end(data);
}
};5. Body Parsing (Reading POST data)
req.on("data", (chunk) => (body += chunk));
req.on("end", () => {
req.body = JSON.parse(body);
});Node.js sends data in chunks, we collect them and parse.
Usage
As npm package
const { createApp } = require("dj-express");
const app = createApp();
// Middleware
app.use((req, res, next) => {
console.log(req.method, req.url);
next();
});
// Routes
app.get("/", (req, res) => {
res.json({ message: "Hello!" });
});
app.get("/users/:id", (req, res) => {
res.json({ userId: req.params.id });
});
app.post("/data", (req, res) => {
res.json(req.body);
});
app.listen(3000);As local module
const { createApp } = require("./index");
// ... rest of the code same as aboveTest Commands
# GET request
curl http://localhost:3000/
# Route params
curl http://localhost:3000/users/123
# Query params
curl "http://localhost:3000/search?q=express"
# POST with body
curl -X POST -H "Content-Type: application/json" -d '{"name":"John"}' http://localhost:3000/dataFeatures
| Feature | How it works |
| -------------- | ------------------------------------------------ |
| Routing | Routes stored in array, matched by path & method |
| Middleware | Array of functions, next() calls the next one |
| req.params | Extracted from /users/:id pattern |
| req.query | Parsed from ?key=value |
| req.body | Parsed from POST request body |
| res.send() | Auto-detects JSON vs text |
| res.json() | Sends JSON response |
| res.status() | Sets status code |
Learn More
License
MIT
