Embedded HTTP Web Server
Embedded asynchronous Web Server API for serving local browser dashboards, static assets, and custom REST JSON endpoints directly from the microcontroller in KryonOS.
Embedded Web Server API: HttpServer
The HttpServer (or System.httpServer) object allows JavaScript applications to host a multi-client HTTP server on the ESP32. It can serve static web pages from flash or SD card and process dynamic REST API requests.
Embedded server source code is available in the KryonOS GitHub Repository.
1. Server Lifecycle
HttpServer.begin(port)
Starts listening for incoming HTTP connections on the designated TCP port.
- Parameters:
port(Integer, optional, default80). - Returns:
Boolean(trueif server listening).
HttpServer.stop()
Stops the server and closes all active client connections.
- Parameters: None
- Returns:
Boolean
2. Route Handling
HttpServer.on(method, path, handler)
Registers an endpoint callback for a specific HTTP method and path.
- Parameters:
method(String):"GET","POST","PUT", or"DELETE".path(String): Route URL path (e.g."/api/status").handler(Function): Callback functionfunction(req, res).
Request Object (req)
| Property | Type | Description |
|---|---|---|
req.url | String | Raw request path and query string |
req.query | Object | Parsed URL query parameters (e.g. req.query.mode) |
req.body | String | Raw body content from POST / PUT requests |
req.headers | Object | Request headers dictionary |
Response Object (res)
res.status(code): Sets HTTP response status code (default200). Returnsresfor chaining.res.setHeader(name, value): Sets a response header.res.send(content): Sends raw text or HTML body.res.json(object): Serializes an object to JSON and setsContent-Type: application/json.
3. Static File Serving
HttpServer.serveStatic(urlPrefix, fsPath)
Maps an entire folder on /local/ or /sd/ to a URL route prefix. Automatically sets appropriate MIME types (.html, .css, .js, .png, .svg).
- Parameters:
urlPrefix(String): e.g."/"or"/assets"fsPath(String): e.g."/sd/www/"or"/local/dashboard/"
- Returns:
Boolean
Complete Example: Web-Controlled Device Dashboard
// Start HTTP server on port 80
HttpServer.begin(80);
// 1. Serve static HTML/CSS web frontend from SD card
HttpServer.serveStatic("/", "/sd/www/");
// 2. REST API: Device telemetry endpoint
HttpServer.on("GET", "/api/status", function(req, res) {
var info = System.getInfo();
res.json({
os: "KryonOS",
uptimeSeconds: Math.floor(System.millis() / 1000),
freeHeap: info.freeRAM,
temperature: System.getTemperature()
});
});
// 3. REST API: Control GPIO pin
HttpServer.on("POST", "/api/gpio", function(req, res) {
try {
var data = JSON.parse(req.body);
var pin = data.pin;
var state = data.state === 1 ? System.gpio.HIGH : System.gpio.LOW;
System.gpio.pinMode(pin, System.gpio.OUTPUT);
System.gpio.digitalWrite(pin, state);
res.json({ success: true, pin: pin, state: data.state });
} catch(e) {
res.status(400).json({ error: "Invalid JSON body" });
}
});
System.print("HTTP Server running on http://" + System.getIPAddress());