Tour · Step 11 of 11
A web server
The standard library includes net.http, an HTTP/1.1 server and client.
A complete web server:
seed net.http
plant app = http.server()
app.get("/", (req) => http.html("<h1>Hello from Plumlang</h1>"))
app.get("/hello/:name", (req) => http.text("Hello, {}!"))
drop("Listening on http://localhost:8080")
app.listen(8080)Run it, and open http://localhost:8080/hello/you in a browser.
http.server()makes a server;app.get(pattern, handler)adds a route. A handler takes the request and returns a response.:namein a pattern matches one part of the path, and the request'sparamsmap holds what it matched.http.text,http.htmlandhttp.jsonmake responses.- Each connection is served by a task of its own, so one slow client holds up nobody.
Query strings and JSON
app.get("/add", (req) => {
plant a = int(req.query.get("a") ?? "0")
plant b = int(req.query.get("b") ?? "0")
return http.json({"a": a, "b": b, "sum": a + b})
})/add?a=2&b=40 answers {"a":2,"b":40,"sum":42}.
The client, and a server that tests itself
http.get fetches a URL. Here the server starts in the background on a
free port, asks itself a few questions, and stops, which is how the
examples in the repository test themselves:
seed net.http
plant app = http.server()
app.get("/hello/:name", (req) => http.text("Hello, {}!"))
app.get("/add", (req) => {
plant a = int(req.query.get("a") ?? "0")
plant b = int(req.query.get("b") ?? "0")
return http.json({"a": a, "b": b, "sum": a + b})
})
plant port = app.start(0, host: "127.0.0.1")
plant base = "http://127.0.0.1:{}"
drop(http.get(base + "/hello/garden").body)
drop(http.get(base + "/add?a=2&b=40").body)
drop(http.get(base + "/missing").status)
app.stop()Hello, garden!
{"a":2,"b":40,"sum":42}
404The net.http page covers the rest:
middleware, static files, HTTPS, and the client's post and post_json.
Where to go next
You've seen most of Plumlang. From here:
- The language reference describes every part of the language exactly.
- The standard library documents each module.
- The examples in the repository are small, complete programs: a bank, shapes, a to-do list, an echo server and a REST API.
- Questions and ideas are welcome in GitHub Discussions.