http.handle()
http.handle(handler, encrypt)
Registers a request handler, turning the script into a JSON web server at /app/<url>.
Registers a callback that serves requests to http://<device IP>/app/<url>.
The handler receives a request object (with req.url set to the path after
/app/) and returns the response body string — build JSON responses with
string(obj).
Register the handler once, then keep the script alive with a run() loop.
Arguments
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| handler | function | yes | — | Callback function(req){ ... return responseString; }. req.url is the path after /app/. |
| encrypt | boolean | yes | — | Require AES end-to-end encryption (key from setEnv("http.aesKey", …)). |
Example
function handleHttp(req){
if(req.url === 'abc'){
return string({ batteryVoltage: 14.8, io: io, uptime: now(), epoch: datetime().epoch });
}
return "Invalid Request URL, try /app/abc";
}
http.handle(handleHttp, false);
while(run()){ waitMS(1000); }