Skip to content

rayquiro.web

This content is for v0.2.1. Switch to the latest version for up-to-date documentation.

The rayquiro.web module provides a micro-web framework compiled natively into the RayQuiro binary. It allows you to build REST APIs, static web servers, and webhooks with zero external dependencies.

import "rayquiro.web";

MethodSignatureDescription
web.serverweb.server()Creates a new HTTP server instance.
app.getapp.get(path, handler)Registers an HTTP GET route.
app.postapp.post(path, handler)Registers an HTTP POST route.
app.putapp.put(path, handler)Registers an HTTP PUT route.
app.deleteapp.delete(path, handler)Registers an HTTP DELETE route.
app.staticapp.static(prefix, dir)Serves static assets from a local folder.
app.listenapp.listen(port)Starts the server listening on the specified port.
app.stopapp.stop()Gracefully shuts down the web server.

Inside a route handler fn(req, res):

FieldTypeDescription
req.pathstringThe requested URL pathname.
req.methodstringHTTP verb ("GET", "POST", etc.).
req.bodystringRaw request body string.
req.headersobjectKey-value map of incoming HTTP headers.
req.queryobjectParsed query string parameters.
MethodSignatureDescription
res.sendres.send(text)Sends plain text with status 200.
res.htmlres.html(html)Sends HTML text with text/html; charset=utf-8.
res.jsonres.json(obj)Serializes object to JSON with application/json.
res.statusres.status(code)Sets the HTTP response status code.
res.headerres.header(name, val)Sets a custom response header.

import "rayquiro.web";
var app = web.server();
app.static("/public", "./public");
var items = [
{ id: 1, name: "Mechanical Keyboard", price: 120 },
{ id: 2, name: "Wireless Mouse", price: 65 }
];
app.get("/api/items", fn(req, res) {
res.json({ success: true, count: len(items), data: items });
});
app.post("/api/items", fn(req, res) {
try {
var payload = json.parse(req.body);
var new_item = {
id: len(items) + 1,
name: payload["name"],
price: payload["price"]
};
items.push(new_item);
res.status(201);
res.json({ success: true, item: new_item });
} catch (err) {
res.status(400);
res.json({ success: false, error: "Invalid JSON format" });
}
});
app.get("/", fn(req, res) {
res.html("<h1>RayQuiro Web Server</h1><p>Visit /api/items for REST data.</p>");
});
var port = 8080;
app.listen(port);
print("Web server running at http://localhost:" + str(port));