@acai/router 中文文档教程

发布于 3年前 浏览 27 项目主页 更新于 3年前

Açai Router Module

https://gitlab.com /acaijs/router.git https: //www.npmjs.com/package/@acai/router https://www.npmjs.com/package/@acai/router https://www.npmjs.com/package/@acai/router

此存储库包含 Açai 框架添加的路由器模块。 它负责创建路由列表并与 url 路径进行匹配,将信息传回给您。

Usage

import { route, router } from "@acai/router";

// list routes available
route.post("/register", "controllers/auth@register");
route.post("/login", "controllers/auth@login");

route.group("/user", () => {
  route.get("/", "controllers/user@show");
  route.patch("/", "controllers/user@update");
});

// Use the router to match it
const selectedRouteInfo = router("url/path/here", "GET", route.build());

Grouping

您可以对路由进行分组,这将在它们内部进行回调,使用它们的上下文。 组可以嵌套。

import { route } from "@acai/router";

route.group("/users", () => {
  route.get("/", "controllers/user@index"); // this route will be /users -> controllers/user@index

  route.group("/auth", () => {
    route.get("/", "controllers/user@show"); // this route will be /users/auth -> controllers/user@show
    route.patch("/", "controllers/user@update");
  });
});

HTTP Methods

您可以使用方法将 HTTP 方法绑定到路由。

import { route } from "@acai/router";

// list routes available
route("/", "any/route"); // equivalent of route.any
route.get("/", "get/route");
route.post("/", "post/route");
route.put("/", "put/route");
route.patch("/", "patch/route");
route.delete("/", "delete/route");
route.any("/", "any/route"); // doesn't care about http method

// if you wish to use multiple http methods for the same route, you can use many
route.many(["PUT", "PATCH"], "/", "put/and/patch/routes");

// you can define a route variable using
route("/{variableName}", "any/route/with/variable");
// and an optional variable with
route("/{variableName?}", "any/route/with/optional/variable");

// you can also pass an callback to the file parameter
route("/", () => {});

Options

有时您希望将附加信息传递给路由,例如中间件或其他任何东西。 您可以使用选项将额外信息绑定到上下文。 对象和数组将自动连接,如果您想避免这种行为,请在选项键前加上 ! 前缀。 选项还将防止数组中的值重复

import { route } from "@acai/router";

// list routes available
route.options({ middleware: ["auth", "admin"] }, () => {
  // routes inside of here will inherit the parents options

  // prefixing an option with ! will overwrite it
  route.options({ "!middleware": ["auth"] }, () => {
    // middleware value: ["auth"]
  });
});

Macro/Use

宏是一组可重用的代码逻辑,您可以使用它来创建具有通用模式的路由。 默认情况下它提供一个资源宏。 您还可以通过定义一个具有相同名称的新宏来覆盖已经定义的宏。

import { route } from "@acai/router";

// define macro that can be used
route.macro("related", (name: string, controller: string) => {
  route.group(name, () => {
    route.get("/", `${controller}@index`);
    route.put("/", `${controller}@set`);
    route.post("/", `${controller}@add`);

    route.group("/{id}", () => {
      route.get("/", `${controller}@show`);
      route.patch("/", `${controller}@edit`);
      route.delete("/", `${controller}@delete`);
    });
  });
});

// Use macro
route.use("related", "posts", "controllers/post.controller");
route.use("related", "comments", "controllers/comment.controller");

Resource macro

urlmethodcontroller
"/<name>"GET"@index"
"/<name>"POST"@store"
"/<name>/{id}"GET"@show"
"/<name>/{id}"PATCH | PUT"@update"
"/<name>/{id}"DELETE"@destroy"

Support

你有问题吗? 请在我们的 main repo 上打开一个问题。

License

BSD 条款 3

版权所有 (c) 2021 The Nuinalp 作者。 保留所有权利。
此源代码的使用受 BSD 样式许可证的约束,该许可证可在 LICENSE 文件中找到。

Açai Router Module

https://gitlab.com/acaijs/router.git https://www.npmjs.com/package/@acai/router https://www.npmjs.com/package/@acai/router https://www.npmjs.com/package/@acai/router

This repository contains the router module added by the Açai Framework. This is responsible for creating the routes list and doing a match with a url path, passing the information back to you.

Usage

import { route, router } from "@acai/router";

// list routes available
route.post("/register", "controllers/auth@register");
route.post("/login", "controllers/auth@login");

route.group("/user", () => {
  route.get("/", "controllers/user@show");
  route.patch("/", "controllers/user@update");
});

// Use the router to match it
const selectedRouteInfo = router("url/path/here", "GET", route.build());

Grouping

You can group routes, that will make the callbacks inside of them, use their context. Groups can be nested.

import { route } from "@acai/router";

route.group("/users", () => {
  route.get("/", "controllers/user@index"); // this route will be /users -> controllers/user@index

  route.group("/auth", () => {
    route.get("/", "controllers/user@show"); // this route will be /users/auth -> controllers/user@show
    route.patch("/", "controllers/user@update");
  });
});

HTTP Methods

You can use methods to bind HTTP methods to routes.

import { route } from "@acai/router";

// list routes available
route("/", "any/route"); // equivalent of route.any
route.get("/", "get/route");
route.post("/", "post/route");
route.put("/", "put/route");
route.patch("/", "patch/route");
route.delete("/", "delete/route");
route.any("/", "any/route"); // doesn't care about http method

// if you wish to use multiple http methods for the same route, you can use many
route.many(["PUT", "PATCH"], "/", "put/and/patch/routes");

// you can define a route variable using
route("/{variableName}", "any/route/with/variable");
// and an optional variable with
route("/{variableName?}", "any/route/with/optional/variable");

// you can also pass an callback to the file parameter
route("/", () => {});

Options

Sometimes you wish to pass additional information to a route, such as a middleware, or anything else. You can bind extra information to the context with options. Objects and arrays will automaticly be joined, if you want to avoid this behaviour, prefix the option key with !. Options will also prevent value duplication in arrays

import { route } from "@acai/router";

// list routes available
route.options({ middleware: ["auth", "admin"] }, () => {
  // routes inside of here will inherit the parents options

  // prefixing an option with ! will overwrite it
  route.options({ "!middleware": ["auth"] }, () => {
    // middleware value: ["auth"]
  });
});

Macro/Use

Macro is a bundle of reusable code logic you can use to create routes with a common pattern. By default it provides a resource macro. You can also overwrite an already defined macro by just defining a new one with the same name.

import { route } from "@acai/router";

// define macro that can be used
route.macro("related", (name: string, controller: string) => {
  route.group(name, () => {
    route.get("/", `${controller}@index`);
    route.put("/", `${controller}@set`);
    route.post("/", `${controller}@add`);

    route.group("/{id}", () => {
      route.get("/", `${controller}@show`);
      route.patch("/", `${controller}@edit`);
      route.delete("/", `${controller}@delete`);
    });
  });
});

// Use macro
route.use("related", "posts", "controllers/post.controller");
route.use("related", "comments", "controllers/comment.controller");

Resource macro

urlmethodcontroller
"/<name>"GET"@index"
"/<name>"POST"@store"
"/<name>/{id}"GET"@show"
"/<name>/{id}"PATCH | PUT"@update"
"/<name>/{id}"DELETE"@destroy"

Support

Do you have a question? Please open an issue on our main repo.

License

BSD Clause 3

Copyright (c) 2021 The Nuinalp Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文