HTTP Server
REK has first-class support for building HTTP servers. The http module makes it easy to create web applications.
Basic Server
import http
let app = http.new()
app.get("/", fn(req, res) {
res.text("Hello from REK!")
})
app.listen(3000)
log("Server running on port 3000")
Routing
Support for common HTTP methods: get, post, put, delete.
app.post("/users", fn(req, res) {
// Handle user creation
res.json({ "status": "created" })
})
Request Object
The req object contains information about the incoming request.
req.body: The request body.req.headers: Map of request headers.req.method: HTTP method (GET, POST, etc.).req.url: The request URL.
Response Object
The res object is used to send data back to the client.
res.text(string): Send plain text.res.json(object): Send a JSON response.res.status(code): Set the HTTP status code (returnsresfor chaining).
app.get("/error", fn(req, res) {
res.status(500).text("Something went wrong")
})