use *;
Get started
Paste the code above into src/main.rs, run cargo run, and open http://localhost:3000.
No Rust yet? Install it with one command from rustup.rs.
Everything you need, in one example
use *;
// Read JSON and send JSON
async
// Middleware: runs before the route, like app.use(fn)
async
Express → RustyWeb
| Express | RustyWeb |
|---|---|
req.params.id |
req.param("id") |
req.query.q |
req.query("q") |
req.body |
req.json()? or req.form()? |
req.cookies.user |
req.cookie("user") |
res.send("Hi") |
return "Hi" |
res.json({ ok: true }) |
return Json(json!({ "ok": true })) |
res.status(404).send("Nope") |
return (StatusCode::NOT_FOUND, "Nope") |
res.redirect("/login") |
return Redirect::to("/login") |
res.sendFile(path) |
return File::send(path).await |
res.cookie("user", "ada") |
res.with_cookie(Cookie::new("user", "ada")) |
app.use(fn) |
app.middleware(fn) |
express.static("public") |
static_files("public") |
express.Router() |
Router::new(), then app.mount("/api", router) |
next() |
next.run(req).await |
| error handler | app.on_error(...) |
More in the Express guide.
Good to know
async move: if the compiler asks you to addmove, add it.?: means "if this failed, stop and send the error". For example bad JSON becomes a400automatically. Use it in functions that returnResult<...>.- Auto-restart when you save, like
nodemon:cargo install cargo-watch, thencargo watch -x run. - Going live: use
app.listen("0.0.0.0:3000"), build withcargo build --release, and run./target/release/my-app.
Built in
Logging, JSON, forms, cookies, static files, file downloads, custom error pages, browser
caching, protection against slow or oversized requests, and graceful shutdown. A bug in one
request returns a 500 and the server keeps running.
Learn more
- Express guide: every common Express call, side by side
- Reference: all features and settings
- Writing packages: share middleware and routes as crates
- Examples: run one with
cargo run --example hello - API documentation