Web server example
On this page
This example serves a directory with matchbox --serve. It has BoxLang Markup (.bxm) pages, a static CSS file, and the url, form, cookie, session and cgi scopes. The Web server guide covers the server in full.
Project structure
index.bxm: Home page. Counts visits in the session and prints the time.about.bxm: Printscgi.server_port.contact.bxm: Readsurl.nameand shows the session id from the cookie scope.styles.css: A static file, served astext/css.
How it works
Every .bxm or .bxs file under the webroot is rendered as a template when you request it. Other files are sent as they are. A request for a directory is answered by its index.bxm or index.bxs.
Each page render starts with these scopes:
url: query-string parameters.form: fields of a URL-encoded POST body.cookie: request cookies, includingMBX_SESSION_ID.session: a struct held in server memory for eachMBX_SESSION_IDcookie. Values are kept as strings.cgi:request_method,script_name,path_info,server_name,server_port,http_host,content_typeandcontent_length.
The server is built on Axum and Tokio and runs the same VM as the matchbox command.
Running the example
- From the
docs/examplesdirectory of the MatchBox source tree, or any directory that contains a copy ofweb_server:matchbox --serve --webroot web_server --port 8080 - Open
http://localhost:8080. - Visit
http://localhost:8080/contact.bxm?name=MatchBoxto see URL parameter handling. - Reload the home page. The visit count in
session.visitCountgoes up each time your browser sends the same session cookie.
The server listens on 127.0.0.1 unless you pass --host 0.0.0.0.
[!note] Template tags Use
<bx:output>to evaluate#expression#in markup. This example keeps branching in<bx:script>and renders the result with one output block.