serez-http
HTTP and WebSocket server for Serez-Code — an Express/Flask-style API
with routing, middleware, rate limiting and CORS, written in pure .sz. Build a REST API in a
few lines and run it with sz:
import "serez-http"
const app = new App()
app.GET("/", fn(req, res) {
let r <string, any> = ({"message", "hello from serez-http"})
res.json(r)
})
app.GET("/user/:id", fn(req, res) {
let r <string, any> = ({"user_id", req["params"]["id"]})
res.json(r)
})
app.POST("/echo", fn(req, res) {
res.json(JSON.parse(req["body"]))
})
app.listen(3000, fn() {
out "server running on port 3000"
})
Install
sz install serez-http
The library declares the Socket and Time permissions itself — your app's serez.json does
not need to add them.
Quick use
- Routes:
app.GET / app.POST / app.PUT / app.delete(path, fn(req, res) {...}). Patterns support:paramsegments, captured inreq["params"]. (getanduseare Serez-Code keywords — that's why the verbs are uppercase and middleware isaddMw.) - Request:
reqis a<string, any>dict —req["method"],req["path"],req["query"],req["params"],req["body"],req["headers"],req["ip"],req["authorization"], … - Response: chainable —
res.status(404).json(obj), plussend,header,cookie,redirect,sendfile,download. - Middleware:
app.addMw(fn(req, res, next) {...})— callnext()to continue the chain; global error handler viaapp.error(fn(err, req, res) {...}). - Security: built-in
app.rateLimit(ip, max, seconds)and acorsMiddleware(origins, methods, headers)factory. - WebSocket:
app.ws(path, fn(req, ws) { ws.on("message", ...); ws.listen() }). - Single-threaded (one connection at a time) and bound to
127.0.0.1— for deployment with external access, package it with serez-apipack.
Documentation
- serez-http reference — the request/response objects, middleware, security helpers and WebSocket, with full examples, on the Serez-Code site.
- Build a REST API — step-by-step tutorial from
sz installto a deployed, Dockerized API. - Serve a model as an API — serez-ai + serez-http together.