HTTP Routing
Build modular, Express.js-style REST APIs with BanglaCode's powerful routing system using pure Banglish keywords.
Overview
BanglaCode provides an Express.js-inspired router system that allows you to organize your HTTP endpoints into modular, reusable components. This makes it easy to build complex web applications with clean, maintainable code structure.
Key Features
- ✅ Modular Routing - Organize routes in separate files
- ✅ All HTTP Methods - GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS support
- ✅ Router Mounting - Mount sub-routers on paths
- ✅ Method Chaining - Define multiple routes fluently
- ✅ Pure Banglish - Bengali keywords throughout
Basic Router
Create a basic HTTP router with multiple endpoints:
router_banao() - Create Router
// Create new router
dhoro app = router_banao();
// Define GET route (আনা - fetch)
app.ana("/", kaj(req, res) {
uttor(res, "Welcome to BanglaCode!");
});
// Define POST route (পাঠানো - send)
app.pathano("/submit", kaj(req, res) {
json_uttor(res, {"message": "Data received"}, 201);
});
// Start server with router
server_chalu(3000, app);HTTP Methods
All 7 standard HTTP methods are supported with pure Banglish keywords:
ana() - GET (আনা - fetch)
app.ana("/users", kaj(req, res) {
dhoro users = [
{"id": 1, "name": "Ankan"},
{"id": 2, "name": "Rahim"}
];
json_uttor(res, {"users": users});
});pathano() - POST (পাঠানো - send)
app.pathano("/users", kaj(req, res) {
// In real app, would parse req["body"]
dhoro newUser = {"id": 3, "name": "New User"};
json_uttor(res, {"user": newUser}, 201);
});bodlano() - PUT (বদলানো - update/change)
app.bodlano("/users/update", kaj(req, res) {
json_uttor(res, {
"message": "User updated successfully"
});
});mujhe_felo() - DELETE (মুছে ফেলো - remove)
app.mujhe_felo("/users/delete", kaj(req, res) {
uttor(res, "User deleted", 204);
});songshodhon() - PATCH (সংশোধন - modify)
app.songshodhon("/users/modify", kaj(req, res) {
json_uttor(res, {"message": "User modified"});
});matha() - HEAD (মাথা - retrieve headers)
app.matha("/users", kaj(req, res) {
res["headers"] = {"Content-Type": "application/json"};
res["status"] = 200;
});nirdharon() - OPTIONS (নির্ধারণ - determine options)
app.nirdharon("/users", kaj(req, res) {
res["headers"] = {"Allow": "GET, POST, PUT, DELETE, PATCH"};
res["status"] = 200;
});Modular Routing
The real power of BanglaCode's routing system is the ability to organize routes into separate modules, just like Express.js.
Step 1: Create Route Modules
File: routes_auth.bang
// Authentication Routes Module
dhoro authRouter = router_banao();
authRouter.pathano("/login", kaj(req, res) {
json_uttor(res, {
"token": "jwt_token_here",
"user": {"name": "Ankan"}
});
});
authRouter.pathano("/register", kaj(req, res) {
json_uttor(res, {
"message": "User registered successfully"
}, 201);
});
authRouter.pathano("/logout", kaj(req, res) {
json_uttor(res, {"message": "Logged out"});
});
// Export router
pathao authRouter;File: routes_users.bang
// User Management Routes Module
dhoro usersRouter = router_banao();
dhoro users = [
{"id": 1, "name": "Ankan"},
{"id": 2, "name": "Rahim"}
];
usersRouter.ana("/", kaj(req, res) {
json_uttor(res, {"users": users});
});
usersRouter.pathano("/", kaj(req, res) {
json_uttor(res, {
"message": "User created"
}, 201);
});
usersRouter.mujhe_felo("/delete", kaj(req, res) {
json_uttor(res, {"message": "User deleted"});
});
// Export router
pathao usersRouter;Step 2: Mount Routers in Main App
File: main.bang
// Import route modules
dhoro authRouter = ano("routes_auth.bang");
dhoro usersRouter = ano("routes_users.bang");
// Create main app
dhoro app = router_banao();
// Main routes
app.ana("/", kaj(req, res) {
json_uttor(res, {
"message": "Welcome to API",
"endpoints": {
"auth": "/api/auth",
"users": "/api/users"
}
});
});
// Mount sub-routers using bebohar (ব্যবহার - use/mount)
app.bebohar("/api/auth", authRouter);
app.bebohar("/api/users", usersRouter);
// Start server
dekho("Server starting on port 3000...");
server_chalu(3000, app);Available Routes
After mounting, these routes are available:
GET /- Main welcome messagePOST /api/auth/login- User loginPOST /api/auth/register- User registrationPOST /api/auth/logout- User logoutGET /api/users- Get all usersPOST /api/users- Create new userDELETE /api/users/delete- Delete user
Method Chaining
Define multiple routes fluently with method chaining:
dhoro app = router_banao();
app
.ana("/", kaj(req, res) {
uttor(res, "Home");
})
.ana("/about", kaj(req, res) {
uttor(res, "About");
})
.pathano("/submit", kaj(req, res) {
uttor(res, "Submitted");
})
.bodlano("/update", kaj(req, res) {
uttor(res, "Updated");
});Request & Response Objects
Request Object (req)
The request object contains information about the incoming HTTP request:
app.ana("/inspect", kaj(req, res) {
dhoro method = req["method"]; // "GET"
dhoro path = req["path"]; // "/inspect"
dhoro query = req["query"]; // Query string
dhoro headers = req["headers"]; // Request headers
dhoro body = req["body"]; // Request body
json_uttor(res, {
"method": method,
"path": path,
"query": query
});
});Response Object (res)
Use helper functions to send responses:
uttor() - Simple Response
// Basic response
uttor(res, "Hello, World!");
// With status code
uttor(res, "Not Found", 404);
// With content type
uttor(res, "Hello", 200, "text/plain");json_uttor() - JSON Response
// JSON response (auto sets content-type)
json_uttor(res, {"message": "Success"});
// With status code
json_uttor(res, {"error": "Not Found"}, 404);Complete Example
A full modular REST API with authentication and users:
// File: main.bang - Complete Modular API
// Import modules
dhoro authRouter = ano("routes_auth.bang");
dhoro usersRouter = ano("routes_users.bang");
// Create app
dhoro app = router_banao();
// Health check
app.ana("/health", kaj(req, res) {
json_uttor(res, {
"status": "healthy",
"uptime": somoy()
});
});
// API info
app.ana("/api", kaj(req, res) {
json_uttor(res, {
"version": "1.0.0",
"endpoints": {
"auth": "/api/auth (login, register)",
"users": "/api/users (CRUD)"
}
});
});
// Mount routers
app.bebohar("/api/auth", authRouter);
app.bebohar("/api/users", usersRouter);
// 404 handler
app.ana("/*", kaj(req, res) {
json_uttor(res, {
"error": "Route not found"
}, 404);
});
// Start server
dekho("🚀 Server starting...");
dekho("📍 http://localhost:3000");
server_chalu(3000, app);Best Practices
1. Organize by Feature
Group related routes in separate modules:
routes_auth.bang- Authenticationroutes_users.bang- User managementroutes_products.bang- Product catalogroutes_orders.bang- Order processing
2. Use Consistent Naming
// Good - descriptive router names
dhoro authRouter = router_banao();
dhoro usersRouter = router_banao();
// Bad - unclear names
dhoro r1 = router_banao();
dhoro temp = router_banao();3. Mount on Logical Paths
// Good - grouped by API version and resource
app.bebohar("/api/v1/auth", authRouter);
app.bebohar("/api/v1/users", usersRouter);
// Bad - inconsistent paths
app.bebohar("/auth", authRouter);
app.bebohar("/api/users", usersRouter);4. Export from Modules
// At end of route module file
pathao authRouter; // Always export your routerAPI Reference
router_banao()
Returns: Router object
Description: Creates a new Express-style router instance
router.ana(path, handler)
Method: GET (আনা - fetch)
path(String) - Route pathhandler(Function) - Request handler- Returns: Router (for chaining)
router.pathano(path, handler)
Method: POST (পাঠানো - send)
path(String) - Route pathhandler(Function) - Request handler- Returns: Router (for chaining)
router.bodlano(path, handler)
Method: PUT (বদলানো - update/change)
path(String) - Route pathhandler(Function) - Request handler- Returns: Router (for chaining)
router.mujhe_felo(path, handler)
Method: DELETE (মুছে ফেলো - remove)
path(String) - Route pathhandler(Function) - Request handler- Returns: Router (for chaining)
router.songshodhon(path, handler)
Method: PATCH (সংশোধন - modify)
path(String) - Route pathhandler(Function) - Request handler- Returns: Router (for chaining)
router.matha(path, handler)
Method: HEAD (মাথা - retrieve headers)
path(String) - Route pathhandler(Function) - Request handler- Returns: Router (for chaining)
router.nirdharon(path, handler)
Method: OPTIONS (নির্ধারণ - determine options)
path(String) - Route pathhandler(Function) - Request handler- Returns: Router (for chaining)
router.bebohar(mountPath, subRouter)
Method: Mount sub-router (ব্যবহার - use/mount)
mountPath(String) - Base path for sub-routersubRouter(Router) - Router to mount- Returns: Router (for chaining)
server_chalu(port, handler)
port(Number) - Port to listen onhandler(Function OR Router) - Request handler or router
Path Parameters
Use :name segments in route paths. Values are available in req["params"] as a MAP.
Query String Parsing
req["query"] is a parsed MAP. Use req["query_raw"] for the raw string.
Auto JSON Body Parsing
When the request has Content-Type: application/json, req["json"] is auto-parsed. Otherwise it is khali.
Middleware (majhe - মাঝে)
Runs before every route handler. Call agorao() (আগাও = go forward) to pass to the next layer.
CORS (cors_chharpao - ছাড়পাও)
Enables Cross-Origin Resource Sharing. Call before defining routes.
Static File Serving (file_dao - ফাইল দাও)
Cookie Handling
Read cookies from req["kukis"]. Set cookies with kuki_rakho() (কুকি রাখো).
Redirect (ghurao - ঘোরাও)
HTML File Response (html_uttor - HTML উত্তর)
Error Middleware (bhul_sambhalo - ভুল সামলাও)
Catches errors returned by route handlers. Register after all routes.