javascript - 如何使用一个主路由创建多个 html 页面?
问题描述
我正在尝试创建一个网站,该网站具有多个社区供稿,例如一个用于保龄球,另一个用于扑克,如下所示:localhost:8088/communities/bowling & localhost:8088/communities/poker。
我使用 actix 作为在 Rust 编程语言下运行的网络服务器。
有没有一种方法可以让在 localhost:8088/communities 下运行的所有地址都遇到相同的 Web 文件,这样我就可以只有一个主路由?
然后存储像 /bowling 或 /poker 这样的附加标头,以便我可以向服务器提交单独的请求以获取相关的帖子提要?也许我可以在调用网页时将附加标题信息保存在 javascript 变量中?- 就像我去 /poker 时一样,名为 communityType 的变量被设置为 poker。我该怎么做这样的事情?
因为没有人可以为大约 100 多个不同的社区中的每一个制作 HTML 页面。
我在这里先向您的帮助表示感谢!
解决方案
我对这个板条箱不是很熟悉,但是根据文档,您可以使用它#[get("/route/this/function/will/support")]
来定义如何处理路由。假设我写的正确,这应该会以小消息响应,告诉您使用时您在哪个社区路线上。
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)
}
您还可以将其扩展为具有发言权#[get("/communities/{name}/forums")]
或#[get("/communities/{name}/{file}")]
处理所有社区在每个社区基础上拥有的公共路线。
编辑:
听起来您在初始化to use时还需要包含.service(communities_route)
在 main 函数中。如果您直接配置服务,您还可以更好地控制路由的处理方式。App
#[get(x)]
这是他们的hello world 示例中的一个片段。只要您访问除"/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))
我建议在 github 上查看他们的示例。看起来他们为许多不同的用例提供了一堆简洁的示例。
推荐阅读
- java - 如何用逗号分割字符串,但从第二个逗号
- javascript - 从离子选择中获取价值并将其存储在本地存储中
- testrigor - 我们如何使用 TestRigor 对 url 执行复制粘贴操作?
- electron - 发现 Electron 支持哪些 Chrome 标志
- sql - 确定正在使用的 SQL 的风格
- php - 如果文件位于子目录中,PHP 无法多次写入同一个文件
- python - 解释仍然活着的概率矩阵图 BG/NBD 模型
- linq - Mongo DB 在 2 个不同的集合中加入数组
- cytoscape.js - 如何在 cytoscape js 中仅显示第一个深度?
- jasper-reports - Jasper 报表表格无法放入当前页面