Advanced
HTTP Server
BanglaCode can create HTTP servers using the server_chalu (start server) function. This allows you to build web applications and APIs.
Starting a Server
Use server_chalu to start an HTTP server on a specified port:
server.bangbanglacode
| 1 | // Basic HTTP server |
| 2 | server_chalu(8080, kaj(req, res) { |
| 3 | uttor(res, "Namaskar from BanglaCode!"); |
| 4 | }); |
| 5 | |
| 6 | dekho("Server running on port 8080"); |
Run the server and visit http://localhost:8080 in your browser.
Request Object
The request object contains information about the incoming HTTP request:
| Property | Description | Example |
|---|---|---|
method | HTTP method | "GET", "POST" |
path | URL path | "/users" |
query | Query string | "id=123" |
headers | Request headers | Map of headers |
body | Request body | String or parsed JSON |
| 1 | server_chalu(8080, kaj(req, res) { |
| 2 | dekho("Method:", req.method); |
| 3 | dekho("Path:", req.path); |
| 4 | dekho("Query:", req.query); |
| 5 | dekho("Headers:", req.headers); |
| 6 | |
| 7 | uttor(res, "Request received!"); |
| 8 | }); |
Sending Responses
Text Response (uttor)
| 1 | // Basic text response |
| 2 | server_chalu(8080, kaj(req, res) { |
| 3 | uttor(res, "Hello World"); |
| 4 | }); |
| 5 | |
| 6 | // With status code |
| 7 | server_chalu(8080, kaj(req, res) { |
| 8 | uttor(res, "Not Found", 404); |
| 9 | }); |
| 10 | |
| 11 | // With content type |
| 12 | server_chalu(8080, kaj(req, res) { |
| 13 | uttor(res, "<h1>Hello</h1>", 200, "text/html"); |
| 14 | }); |
JSON Response (json_uttor)
| 1 | server_chalu(8080, kaj(req, res) { |
| 2 | dhoro data = { |
| 3 | status: "success", |
| 4 | message: "User created", |
| 5 | user: { |
| 6 | id: 1, |
| 7 | naam: "Rahim" |
| 8 | } |
| 9 | }; |
| 10 | |
| 11 | json_uttor(res, data); |
| 12 | }); |
| 13 | |
| 14 | // With custom status code |
| 15 | server_chalu(8080, kaj(req, res) { |
| 16 | json_uttor(res, {error: "Not found"}, 404); |
| 17 | }); |
Routing
| 1 | server_chalu(8080, kaj(req, res) { |
| 2 | // Simple routing based on path |
| 3 | jodi (req.path == "/") { |
| 4 | uttor(res, "Home Page", 200, "text/html"); |
| 5 | } nahole jodi (req.path == "/about") { |
| 6 | uttor(res, "About Page", 200, "text/html"); |
| 7 | } nahole jodi (req.path == "/api/users") { |
| 8 | json_uttor(res, {users: ["Rahim", "Karim"]}); |
| 9 | } nahole { |
| 10 | uttor(res, "404 Not Found", 404); |
| 11 | } |
| 12 | }); |
Method-based Routing
| 1 | server_chalu(8080, kaj(req, res) { |
| 2 | jodi (req.path == "/api/users") { |
| 3 | jodi (req.method == "GET") { |
| 4 | // List users |
| 5 | json_uttor(res, {users: getAllUsers()}); |
| 6 | |
| 7 | } nahole jodi (req.method == "POST") { |
| 8 | // Create user |
| 9 | dhoro body = json_poro(req.body); |
| 10 | dhoro user = createUser(body); |
| 11 | json_uttor(res, user, 201); |
| 12 | |
| 13 | } nahole { |
| 14 | json_uttor(res, {error: "Method not allowed"}, 405); |
| 15 | } |
| 16 | } nahole { |
| 17 | json_uttor(res, {error: "Not found"}, 404); |
| 18 | } |
| 19 | }); |
Practical Examples
Simple API Server
api_server.bangbanglacode
| 1 | // In-memory data store |
| 2 | dhoro users = []; |
| 3 | dhoro nextId = 1; |
| 4 | |
| 5 | kaj findUser(id) { |
| 6 | ghuriye (dhoro i = 0; i < dorghyo(users); i = i + 1) { |
| 7 | jodi (users[i].id == id) { |
| 8 | ferao users[i]; |
| 9 | } |
| 10 | } |
| 11 | ferao khali; |
| 12 | } |
| 13 | |
| 14 | server_chalu(8080, kaj(req, res) { |
| 15 | // CORS headers for API |
| 16 | res.headers["Access-Control-Allow-Origin"] = "*"; |
| 17 | |
| 18 | // Routes |
| 19 | jodi (req.path == "/api/users" ebong req.method == "GET") { |
| 20 | // List all users |
| 21 | json_uttor(res, { |
| 22 | success: sotti, |
| 23 | data: users, |
| 24 | count: dorghyo(users) |
| 25 | }); |
| 26 | |
| 27 | } nahole jodi (req.path == "/api/users" ebong req.method == "POST") { |
| 28 | // Create user |
| 29 | chesta { |
| 30 | dhoro body = json_poro(req.body); |
| 31 | |
| 32 | dhoro user = { |
| 33 | id: nextId, |
| 34 | naam: body.naam, |
| 35 | email: body.email, |
| 36 | createdAt: somoy() |
| 37 | }; |
| 38 | |
| 39 | dhokao(users, user); |
| 40 | nextId = nextId + 1; |
| 41 | |
| 42 | json_uttor(res, {success: sotti, data: user}, 201); |
| 43 | } dhoro_bhul (e) { |
| 44 | json_uttor(res, {success: mittha, error: "Invalid data"}, 400); |
| 45 | } |
| 46 | |
| 47 | } nahole { |
| 48 | json_uttor(res, {success: mittha, error: "Not found"}, 404); |
| 49 | } |
| 50 | }); |
| 51 | |
| 52 | dekho("API Server running on http://localhost:8080"); |
HTML Web Server
web_server.bangbanglacode
| 1 | kaj renderHTML(title, content) { |
| 2 | ferao "<!DOCTYPE html> |
| 3 | <html> |
| 4 | <head> |
| 5 | <title>" + title + "</title> |
| 6 | <style> |
| 7 | body { font-family: sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; } |
| 8 | h1 { color: #7c3aed; } |
| 9 | </style> |
| 10 | </head> |
| 11 | <body> |
| 12 | " + content + " |
| 13 | </body> |
| 14 | </html>"; |
| 15 | } |
| 16 | |
| 17 | server_chalu(8080, kaj(req, res) { |
| 18 | jodi (req.path == "/") { |
| 19 | dhoro html = renderHTML("Home", " |
| 20 | <h1>Namaskar!</h1> |
| 21 | <p>Welcome to BanglaCode Web Server</p> |
| 22 | <ul> |
| 23 | <li><a href='/about'>About</a></li> |
| 24 | <li><a href='/contact'>Contact</a></li> |
| 25 | </ul> |
| 26 | "); |
| 27 | uttor(res, html, 200, "text/html"); |
| 28 | |
| 29 | } nahole jodi (req.path == "/about") { |
| 30 | dhoro html = renderHTML("About", " |
| 31 | <h1>About Us</h1> |
| 32 | <p>BanglaCode is a Bengali programming language.</p> |
| 33 | <a href='/'>Back to Home</a> |
| 34 | "); |
| 35 | uttor(res, html, 200, "text/html"); |
| 36 | |
| 37 | } nahole { |
| 38 | dhoro html = renderHTML("404", "<h1>Page Not Found</h1>"); |
| 39 | uttor(res, html, 404, "text/html"); |
| 40 | } |
| 41 | }); |
| 42 | |
| 43 | dekho("Web server running on http://localhost:8080"); |
Form Handler
| 1 | server_chalu(8080, kaj(req, res) { |
| 2 | jodi (req.path == "/" ebong req.method == "GET") { |
| 3 | dhoro form = "<!DOCTYPE html> |
| 4 | <html> |
| 5 | <body> |
| 6 | <h1>Contact Form</h1> |
| 7 | <form method='POST' action='/submit'> |
| 8 | <p>Name: <input name='name'></p> |
| 9 | <p>Email: <input name='email' type='email'></p> |
| 10 | <p>Message: <textarea name='message'></textarea></p> |
| 11 | <button type='submit'>Send</button> |
| 12 | </form> |
| 13 | </body> |
| 14 | </html>"; |
| 15 | uttor(res, form, 200, "text/html"); |
| 16 | |
| 17 | } nahole jodi (req.path == "/submit" ebong req.method == "POST") { |
| 18 | // Process form data |
| 19 | dekho("Form submitted:", req.body); |
| 20 | |
| 21 | dhoro response = "<!DOCTYPE html> |
| 22 | <html> |
| 23 | <body> |
| 24 | <h1>Thank You!</h1> |
| 25 | <p>Your message has been received.</p> |
| 26 | <a href='/'>Back</a> |
| 27 | </body> |
| 28 | </html>"; |
| 29 | uttor(res, response, 200, "text/html"); |
| 30 | } |
| 31 | }); |
HTTP Client (anun)
Use anun (meaning "fetch" or "bring") to make HTTP requests:
| 1 | // Simple GET request |
| 2 | dhoro response = anun("https://api.example.com/data"); |
| 3 | dekho(response); |
| 4 | |
| 5 | // Parse JSON response |
| 6 | dhoro data = json_poro(response.body); |
| 7 | dekho(data); |
Enhanced HTTP Client (anun)
anun() now supports all HTTP methods via an optional options map. Backward-compatible: one-argument GET still works.
// GET (unchanged)
dhoro res = anun("https://api.example.com/users");
// POST with JSON body
dhoro res = anun("https://api.example.com/users", {
"method": "POST",
"body": json_banao({"name": "Ankan", "email": "a@b.com"}),
"headers": {"Content-Type": "application/json"}
});
// PUT
dhoro res = anun("https://api.example.com/users/1", {
"method": "PUT",
"body": json_banao({"name": "Updated"})
});
// DELETE
dhoro res = anun("https://api.example.com/users/1", {
"method": "DELETE"
});
dekho("Status:", res["status"]);
dhoro data = json_poro(res["body"]);
Async HTTP Client (anun_async)
proyash kaj createUser(userData) {
dhoro res = opekha anun_async("https://api.example.com/users", {
"method": "POST",
"body": json_banao(userData),
"headers": {"Content-Type": "application/json"}
});
ferao json_poro(res["body"]);
}
dhoro user = opekha createUser({"name": "Ankan"});
dekho("Created:", user);
Request Object Reference
All fields available on req inside route handlers:
| Field | Type | Description |
|---|---|---|
| req["method"] | STRING | HTTP method: GET, POST, PUT, ... |
| req["path"] | STRING | Request path: /users/123 |
| req["ip"] | STRING | Client IP address |
| req["headers"] | MAP | All request headers |
| req["body"] | STRING | Raw request body |
| req["json"] | MAP/NULL | Auto-parsed JSON body (when Content-Type: application/json) |
| req["form"] | MAP/NULL | URL-encoded form data (when Content-Type: application/x-www-form-urlencoded) |
| req["params"] | MAP | Path params: route /users/:id → req["params"]["id"] |
| req["query"] | MAP | Parsed query string: ?q=hi → req["query"]["q"] |
| req["query_raw"] | STRING | Raw query string: "q=hi&page=2" |
| req["kukis"] | MAP | Parsed cookies: req["kukis"]["session"] |
Performance Helpers Reference
| Function | Bengali | Description |
|---|---|---|
| goti_shima(app, max, window) | গতি সীমা | Rate limit: max requests per window seconds per IP |
| sankochon_chalu(app) | সংকোচন চালু | Enable gzip compression |
| somoy_shima(app, secs) | সময় সীমা | Request timeout in seconds |
| akaar_shima(app, bytes) | আকার সীমা | Max request body size in bytes |
| log_chalu(app) | লগ চালু | Enable request logging |
| cors_chharpao(app, opts?) | ছাড়পাও | Enable CORS |
| file_dao(app, url, dir) | ফাইল দাও | Serve static files |
| ghurao(res, url, status?) | ঘোরাও | HTTP redirect (default 302) |
| kuki_rakho(res, name, val, opts?) | কুকি রাখো | Set response cookie |
| html_uttor(res, filepath) | HTML উত্তর | Serve HTML file |
| bhul_sambhalo(app, handler) | ভুল সামলাও | Global error middleware |
Best Practices
- Always validate input - Never trust client data
- Use appropriate status codes - 200 for success, 404 for not found, etc.
- Set correct content types - Especially for JSON and HTML
- Handle errors gracefully - Use
bhul_sambhalo()for centralized error handling - Use JSON for APIs - It's the standard for data exchange
- Enable production features -
cors_chharpao,goti_shima,sankochon_chalu,somoy_shimafor production deployments