A tour of the basic building blocks of an ntex web application: route macros
and resources, path parameters, cookie sessions, streaming responses, static
files, redirects, errors and a custom 404 page. See src/main.rs for detailed
comments.
cargo run -p basicsThe server listens on 127.0.0.1:8080. Static files are resolved relative to
the crate directory, so the example can be started from any directory. Requests
are logged to the console; set RUST_LOG to change the log level.
| Request | Result |
|---|---|
GET / |
302 Found redirect to /welcome |
GET /welcome |
HTML page with a visit counter stored in a cookie session |
GET /favicon |
static/favicon.ico, served with NamedFile |
GET /static/ |
Directory listing of static/; files are served from /static/<file> |
GET /user/bob |
Hello bob! — path parameter extracted with Path<T> |
GET /files/a/b/c |
matched tail: a/b/c — a tail segment matches the rest of the path |
GET /async-body/bob |
Hello bob! sent as a streaming (chunked) body |
GET /test, POST /test, other methods |
200, 405 and 404 — the handler matches on the method |
GET /error |
500 Internal Server Error built with InternalError |
any other GET request |
custom 404 page (static/404.html) |
any other non-GET request |
405 Method Not Allowed |
Try the session counter and the method-based routes with curl:
# the counter increases on each request that sends the session cookie back
curl -c cookies.txt -b cookies.txt http://localhost:8080/welcome
curl -i -X POST http://localhost:8080/test # 405 Method Not Allowed
curl -i -X DELETE http://localhost:8080/test # 404 Not Found
curl -i -X POST http://localhost:8080/not-found # 405 from the default servicecargo test -p basicsCovers parameter routes, redirects, session cookies, static files, custom 404 pages, and error responses.