server
A small web server for local tools and dashboards. app = server.create(); app.get("/items/{id}", fn(req) { return server.json({...}) }); app.listen(8000). Handlers get a req map (method, path, params, query, headers, body, json, form, cookies, ip) and give back text, a map/list (sent as JSON) or a response from server.json/text/html/file/redirect. Safe defaults: listens on 127.0.0.1 only, bodies up to 10 MB, static files can't escape their folder, handler errors become a 500 JSON answer. app.ws(path, fn) serves WebSockets.
This library listens for web requests on your computer, so it needs the TB app. The examples here show the code; run them with tb file.tb.
import server
Example
Needs the TB app on your computer, so there is no Run button here.
import server
let app = server.create()
let notes = [{"id": 1, "text": "check auth.log", "by": "Tanisha"}]
app.get("/", fn(req) {
return server.html("<h1>Notes</h1>")
})
app.get("/notes/{id}", fn(req) {
return notes.filter(fn(n) {
return str(n.id) == req.params.id
})
})
app.post("/notes", fn(req) {
notes.push({"id": len(notes) + 1, "text": req.json.text, "by": "Tanisha"})
return server.json(notes.last(), 201)
})
app.listen(8000) # http://127.0.0.1:8000, this computer only
Functions
json(data, status?, headers?)
returns responseA JSON answer (Content-Type application/json), status 200 unless given.
Visualizehow server.json works
server.json({"ok": true, "items": 3}, 200) gives {"_response": true, "status": 200, "body": "{\"ok\": true, \"items\": 3}", "headers": {"C…server.json({"ok": true, "items": 3}, 201) gives {"_response": true, "status": 201, "body": "{\"ok\": true, \"items\": 3}", "headers": {"C…server.json({"ok": true, "items": 3}, 404) gives {"_response": true, "status": 404, "body": "{\"ok\": true, \"items\": 3}", "headers": {"C…
Real result: worked out by running this example with TB while the site was built.
text(body, status?, headers?)
returns responseA plain-text answer.
Visualizehow server.text works
server.text("pong", 200) gives {"_response": true, "status": 200, "body": "pong", "headers": {"Content-Type": "text/plai…server.text("pong", 202) gives {"_response": true, "status": 202, "body": "pong", "headers": {"Content-Type": "text/plai…server.text("pong", 503) gives {"_response": true, "status": 503, "body": "pong", "headers": {"Content-Type": "text/plai…
Real result: worked out by running this example with TB while the site was built.
html(body, status?, headers?)
returns responseAn HTML page answer (the template library makes good bodies).
Visualizehow server.html works
server.html("<h1>Alerts</h1><p>3 new</p>") gives {"_response": true, "status": 200, "body": "<h1>Alerts</h1><p>3 new</p>", "headers": {"Co…
Real result: worked out by running this example with TB while the site was built.
file(path, options?)
returns responseSend a file from disk, with its type guessed from the name. A missing file gives a 404 answer.
Options
downloada file name like "name.csv". It makes browsers save the file.typethe type to send, like "text/csv".
Visualizehow server.file works
server.file("data/users.csv", {"download": "users.csv"}) gives {"_response": true, "status": 200, "body": b"name,role\x0aAda,admin\x0aBo,user\x0a", "hea…
Real result: worked out by running this example with TB while the site was built.
redirect(url, status?)
returns responseSend the browser somewhere else (status 302 unless given, 301 for permanent).
Visualizehow server.redirect works
server.redirect("/login", 302) gives {"_response": true, "status": 302, "body": b"", "headers": {"Location": "/login"}}server.redirect("/login", 301) gives {"_response": true, "status": 301, "body": b"", "headers": {"Location": "/login"}}
Real result: worked out by running this example with TB while the site was built.
response(status, body?, headers?)
returns responseAny answer you like: server.response(201, "created", {"Location": "/items/7"}).
Visualizehow server.response works
server.response(201, "created", {"Location": "/items/7"}) gives {"_response": true, "status": 201, "body": "created", "headers": {"Location": "/items/7"}}
Real result: worked out by running this example with TB while the site was built.
error(status, message)
returns responseA JSON error answer: {"error": message, "status": status}.
Visualizehow server.error works
server.error(400, "no such note") gives {"_response": true, "status": 400, "body": "{\"error\": \"no such note\", \"status\": 400…server.error(403, "no such note") gives {"_response": true, "status": 403, "body": "{\"error\": \"no such note\", \"status\": 403…server.error(404, "no such note") gives {"_response": true, "status": 404, "body": "{\"error\": \"no such note\", \"status\": 404…server.error(500, "no such note") gives {"_response": true, "status": 500, "body": "{\"error\": \"no such note\", \"status\": 500…
Drawing…
Real result: worked out by running this example with TB while the site was built.
create(options?)
returns appMake a web app.
Options
logprint one line per request (default: true).max_bodythe biggest request body, in bytes (default: 10 MB).corstrue or an origin. It lets browser pages from elsewhere call the API.namea name for the app (text).
The app has
get, post, put, patch, delete, route(path, fn)add a handler for a path.static(prefix, folder)serve the files in a folder.ws(path, fn)serve WebSockets.listen(port?, host?)run the server until Ctrl+C.start(port?, host?)run the server in the background. stop() stops it..port, .url, .runningwhere it runs and whether it is on.
Example
let app = server.create()
app.get("/hello", fn(req) { return "Hi!" })
app.listen(8000)
Visualizehow server.create works
server.create({"log": true, "cors": true}) gives <app>
Illustration: example values, drawn from what the function does.
stop_all()
Stop every running server made by this program.
Visualizehow server.stop_all works
server.stop_all()
No inputs: call it as it is.
Illustration: example values, drawn from what the function does.
escape(text)
returns textText made safe inside HTML (same as template.escape).
Visualizehow server.escape works
server.escape("<script>alert(1)</script>") gives "<script>alert(1)</script>"
Real result: worked out by running this example with TB while the site was built.
parse_query(text)
returns mapRead a query string: parse_query("a=1&b=x%20y") is {"a": "1", "b": "x y"}.
Visualizehow server.parse_query works
server.parse_query("q=ssh%20brute&page=2&debug") gives {"q": "ssh brute", "page": "2", "debug": ""}
Real result: worked out by running this example with TB while the site was built.
Values
| Name | Value | What it is |
|---|---|---|
server.max_body | 10485760 | The default request-body limit in bytes (10 MB). Visualizehow server.max_body works
No inputs: call it as it is. Real value, read from TB while the site was built. |
server.methods | ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"] | The HTTP methods route() accepts. Visualizehow server.methods works
No inputs: call it as it is. Real value, read from TB while the site was built. |