简体   繁体   中英

How to create multiple html pages using one main route?

I'm trying to create a website that has multiple community feeds like one for bowling and another for poker like so: localhost:8088/communities/bowling & localhost:8088/communities/poker.

I use actix as my webserver operating under the Rust programming language.

Is there a way where all addresses operating under localhost:8088/communities encounter the same web files so that I can just have one main route?

Then store the additional header like /bowling or /poker so that I can commit to separate requests to the server for the relevant post feeds? Maybe I can save the additional header information in a javascript variable when the web page is called? -Like when I go to /poker, a variable named communityType gets set to poker. How would I do something like that?

Because there's no way anyone is making HTML pages for each of ~100s different communities.

Thanks for the help in advance!

I'm not very familiar with this crate, but based on the docs it looks like you can use #[get("/route/this/function/will/support")] to define how a route is handled. Assuming that I wrote it correctly, this should respond with small message telling you which community route you are on when using.

use actix_web::{get, web, App, HttpServer, Responder};

#[get("/communities/{name}")]
async fn communities_route(web::Path(name): web::Path<String>) -> impl Responder {
    format!("This is the page for the {:} community!", name)
}

You could also expand it to have routes for say #[get("/communities/{name}/forums")] or #[get("/communities/{name}/{file}")] to handle common routes all communities have on a per community basis.

Edit:

It sounds like you also need to include .service(communities_route) in your main function when initializing the App to use #[get(x)] . You also have better control over how the route is handled if you configure the services directly.

Here is one snippet from their hello world example . It looks like it prints the request on the server so long as you visit any route other than "/index.html" .

async fn index(req: HttpRequest) -> &'static str {
    println!("REQ: {:?}", req);
    "Hello world!"
}

App::new()
.service(web::resource("/index.html").to(|| async { "Hello world!" }))
.service(web::resource("/").to(index))

I recommend looking through their examples on github. It looks like they have a bunch of concise examples for many different use cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM