我目前正在为Oak学习Deno。
我掌握了如何声明指向API端点的静态路由:
import helloWorld from './hello-world.ts';
// [... MORE CODE ...]
router.get('/hello-world', ({ response }: { response: any }) => {
response.status = 200;
response.headers.set('Content-Type', 'text/html');
response.body = helloWorld;
});但我将如何动态路由,例如,请求:
/hello-world-1/hello-world-2/welcome-page-1/welcome-page-2至:
./hello-world/1/page.ts./hello-world/2/page.ts./welcome-page/1/page.ts./welcome-page/2/page.ts假设hello-world和welcome-page页面集都将继续,直到:
/hello-world-n/welcome-page-n我猜我不需要手动将每个请求路由到server.ts中的每个页面
示例:
我非常乐意将router.get()的第一个参数编写为正则表达式通配符(然后在方法中使用arguments[0]获取实际值)。
但是,据我所知,如果该参数不是字符串,则该方法将失败。
发布于 2021-03-04 02:27:21
免责声明:这是在没有测试的情况下拼凑在一起的,也许我的逃避或其他事情弄错了,请随时在评论中纠正我。
通常,使用编码参数以REST方式路由到各个页面,如基本用法示例中所示。相关迷你片段:
router.get("/path/:id", (context) => {}它为您提供了一个param id来查找函数体中的各个页面。
正如在文档中提到的,您可以使用内置的路径对正则表达式模块对路径进行按摩,如下所示:
router.get("/:path-:id(\\d+)", (context) => {}它应该给你相关的部分path (例如hello-world和welcome-page)和id (1,2,.)若要在文件系统上创建实际路径(例如./hello-world/1/page.ts)或以其他方式查找要返回的页面数据,请执行以下操作。
https://stackoverflow.com/questions/66464697
复制相似问题