Standard Library
http
A minimal HTTP server. Open a port, accept clients, read requests, send responses.
Import:
import "http";create_server
Open a listening socket on the given port and return a handle.
int server = create_server(8080);accept_client
Block until a client connects, then return a handle for that client.
int client = accept_client(server);read_request
Parse the incoming HTTP request from a client. The result is an object with method, path, headers, and body fields.
object req = read_request(client);
print(req->method + " " + req->path);send_html
Send an HTML response to the client. The body is written verbatim with a 200 OK and Content-Type: text/html.
send_html(client, "<h1>Hello</h1>");send_response
Send a custom response with a status code and body.
send_response(client, 404, "Not found");close_client
Close a client connection. Most apps will only need this if they manage long-lived connections explicitly.
close_client(client);Putting it together
import "io";
import "http";
import "html";
int server = create_server(8080);
print("Listening on :8080");
while (1) {
int client = accept_client(server);
object req = read_request(client);
send_html(client, router(req, {
"/": h1({ children: ["Hello"] })
}));
}