Web servers
Declare a web server, say what each route does, and start it. Readable HTTP services with JSON responses.
Otter
api is a web server
port is 8080
host is "localhost"
.
when api receives GET at "/health"
respond with "OK"
.
when api receives GET at "/api/users"
respond with "Ada Lovelace" as json
.
when api receives POST at "/api/users" into req
respond with "User created" as json and status 201
.
start api
Each when api receives METHOD at "path" block is a route. into req names the incoming request. respond with sends the reply; as json encodes it, and and status sets the status code. Run a server program with otter serve server.ot, not otter run: it listens on the port the program declares (8080 here), -Port overrides it (otter serve server.ot -Port 9000), and Ctrl+C stops it. Under otter run, the first route stops the program with an error.
Next: web applications