http
HTTP server, routing, middleware, client, and CORS.
benutzen "http". Two server styles: **serve** (one handler for every request) or **listen** + **get/post/put/delete** (path routing).
Handlers receive req with method, path, body, headers, and (on matched routes) params. Return http::response, http::jsonResponse, or http::serveFile.
Pass handlers as named functions or **anonymous functions**: http::get("/", fun(req) { ... }) is idiomatic for short routes.
Wrap handler bodies in **versuch / fang** (try/catch) to catch runtime errors and return 500 without crashing the server. See **Versuch / fang (try/catch)** under Control flow.
Use **http::use** to register middleware. Return a response object to short-circuit; otherwise the chain continues to the route handler.
Client calls are **fetch** and **fetchPost**: not get/post, which register routes.
Requires libcurl at build time.
Anonymous route handlers
Pass a fun(req) directly to http::get or http::post.
benutzen "http";
http::cors(true);
http::get("/", fun(req) {
return http::response(200, "anonymous works!");
});
http::get("/test/:id", fun(req) {
return http::response(200, "id: " + req.params.id);
});
http::listen(8080);Router with middleware
Register routes on a router and stack middleware with http::use.
benutzen "http";
fun logger(req) {
print req.method + " " + req.path;
}
fun home(req) {
return http::response(200, "hello");
}
fun createUser(req) {
var name = http::header(req, "content-type");
return http::jsonResponse(201, "{\"ok\":true}");
}
http::cors(true);
http::use(logger);
http::get("/", home);
http::post("/users", createUser);
http::listen(8080);Route params
Read :id segments from req.params in path patterns.
benutzen "http";
fun showUser(req) {
var id = req.params.id;
return http::response(200, "user " + id);
}
http::get("/users/:id", showUser);
http::listen(8080);Error handling with versuch / fang (try/catch)
Catch handler errors and return a 500 instead of crashing.
benutzen "http";
benutzen "json";
fun predict(req) {
versuch {
var data = json::parse(req.body);
if (data.pixels->len() != 784) {
return http::response(400, "expected 784 pixels");
}
return http::jsonResponse(200, "{\"digit\":3}");
} fang (err) {
return http::response(500, err);
}
}
http::post("/api/predict", predict);
http::listen(8080);Single-handler server
One callback handles every request: good for tiny servers.
| Symbol | Description | Example |
|---|---|---|
serve(port, handler) | Blocking server: every request goes to one handler | benutzen "http";
fun h(req) {
return http::response(200, "ok");
}
http::serve(8080, h); |
Router
Named routes for GET, POST, PUT, and DELETE.
| Symbol | Description | Example |
|---|---|---|
listen(port) | Start server after registering routes | benutzen "http";
http::get("/", home);
http::listen(8080); |
get(path, handler) | Register GET route; `:name` captures params | benutzen "http";
http::get("/users/:id", showUser); |
post(path, handler) | Register POST route | benutzen "http";
http::post("/api/predict", predict); |
put(path, handler) | Register PUT route | benutzen "http";
http::put("/users/:id", updateUser); |
delete(path, handler) | Register DELETE route | benutzen "http";
http::delete("/users/:id", removeUser); |
Middleware
Run shared logic before handlers: logging, auth, etc.
| Symbol | Description | Example |
|---|---|---|
use(middleware) | Run before each route handler; return a response to stop the chain | benutzen "http";
fun auth(req) {
if (http::header(req, "authorization") == nil) {
return http::response(401, "unauthorized");
}
}
http::use(auth); |
Responses
Build status, body, and content-type for handlers.
| Symbol | Description | Example |
|---|---|---|
response(status, body) | Plain-text response (`type` defaults to `text/plain`) | benutzen "http";
return http::response(200, "hello"); |
jsonResponse(status, json) | JSON response (`type` is `application/json`) | benutzen "http";
return http::jsonResponse(200, "{\"ok\":true}"); |
serveFile(status, path) | Read a file; MIME type inferred from extension | benutzen "http";
return http::serveFile(200, "index.html"); |
cors(enabled, origin?) | Add CORS headers; optional origin (default `*`). Handles OPTIONS preflight. | benutzen "http";
http::cors(true, "http://localhost:3000"); |
Client
fetch and fetchPost for outbound HTTP calls.
| Symbol | Description | Example |
|---|---|---|
fetch(url) | HTTP GET → `{ status, body }` | benutzen "http";
var r = http::fetch("https://example.com");
print r.status; |
fetchPost(url, body) | HTTP POST with JSON body → `{ status, body }` | benutzen "http";
var r = http::fetchPost("https://httpbin.org/post", "{}");
print r.status; |
Request helpers
Read headers and other request metadata.
| Symbol | Description | Example |
|---|---|---|
header(req, name) | Read a request header (names are lowercase) | benutzen "http";
var ct = http::header(req, "content-type"); |
Request fields (in handler)
Fields available on the req object in a handler.
| Symbol | Description | Example |
|---|---|---|
req.method | HTTP method string | fun h(req) { print req.method; } |
req.path | URL path (query string stripped on routed requests) | fun h(req) { print req.path; } |
req.body | Request body string | fun h(req) { print req.body; } |
req.headers | Object of lowercase header names → values | fun h(req) { print req.headers.host; } |
req.params | Route capture object (`:id` → `req.params.id`) | fun h(req) { print req.params.id; } |
Response object fields
Fields you set on the response returned from a handler.
| Symbol | Description | Example |
|---|---|---|
status | HTTP status code (number) | var r = http::response(200, "ok");
print r.status; |
body | Response body string | var r = http::response(200, "ok");
print r.body; |
type | Content-Type header value | var r = http::jsonResponse(200, "{}");
print r.type; |