Skip to main content

routing/
routing.rs

1use expressjs::prelude::*;
2
3#[tokio::main]
4async fn main() {
5    let mut app = express();
6
7    // Static route
8    app.get("/", async |_req, res| res.send_text("Welcome!"));
9
10    // Route with one parameter
11    app.get("/users/{id}", async |req, res| {
12        let id = req.params().get("id").unwrap_or("unknown");
13        res.send_text(format!("User ID: {id}"))
14    });
15
16    // Route with multiple parameters
17    app.get(
18        "/posts/{post_id}/comments/{comment_id}",
19        async |req, res| {
20            let post_id = req.params().get("post_id").unwrap_or("unknown");
21            let comment_id = req.params().get("comment_id").unwrap_or("unknown");
22            res.send_text(format!("Post ID: {post_id}, Comment ID: {comment_id}"))
23        },
24    );
25
26    // Catch-all (glob)
27    app.get("/static/{*path}", async |req, res| {
28        let path = req.params().get("path").unwrap_or("");
29        res.send_text(format!("Serving file from: {path}"))
30    });
31
32    app.listen(9000, async |port| {
33        println!("🚀 Server running on http://localhost:{port}/");
34    })
35    .await;
36}