@acai/server 中文文档教程

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

Açai Server Module

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

服务器负责实际处理传入的请求,并将它们返回给用户。 它支持中间件和提供程序。

Usage

import server from "@acai/server";

// we are going to use Açai's router to control our routes, but you can easily overwrite this, will be shown next
const route = server.getRoute();

route.options({ middleware: ["test"] }, () => {
  route.get("/", "file/test@index");
});

const instance = new server();

// Middlewares are optional
instance.addMiddleware("test", (data, next) => next(data));

instance.run();

Overwriting router

如果您不想使用我们的路由器,您可以轻松地覆盖它:

const instance = new server();

instance.setOnRequest((url: string, method: string) => {
  return {
    route: match.path,
    // file to load from the route match
    controller: ".",
    // method to call inside of the file (optional)
    method: "index",
    // extra options, such as middleware, etc
    options: match.options,
    // variables match from the dynamic route
    params: match.variables,
    // query params
    query: query,
    // request body
    fields: content.fields,
    // request files (in case of formdata)
    files: content.files,
  };
});

例如,如果您希望将所有路由指向单个前端,在 SPA 的情况下,您可以使用它。

Providers

提供程序是启动应用程序的主要方式,可帮助您以有组织的方式设置应用程序。

const instance = new server();

class Provider {
  public boot() {
    /* do something */
  }
}

// aliased middleware
instance.addProvider(Provider);

instance.run();

Middlewares

中间件是一个管道,它在请求实际到达您的代码之前运行,对请求内容进行更改,甚至将其释放。 您可以将它们用于身份验证、验证或类似的事情。

const instance = new server();

// aliased middleware
instance.addMiddleware("test", (data, next) => next(data));

// grouped aliased middlewares
instance.addMiddlewares({
  test: (data, next) => next(data),
  auth: (data, next) => "404",
});

// global middleware
instance.addGlobalMiddleware([(data, next) => next(data)]);

instance.run();
  • global middlewares will run in every request of your application
  • aliased middleware are middlewares that you can call through your routes, for privileged requests, and such.

Responses

我们为您提供了一个响应实用程序方法,可以帮助您导入文件、返回请求状态代码等。请记住,服务器会根据您提供的响应进行智能猜测,将 json 转换为实际的 json 等。

import { response } from "@acai/server";

// return this to the server to load a file from the project root
response().view("./views/index.html");
// change the response status code
response().status(201).data({});

Açai Server Module

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

The server is responsible for actually handling incoming requests, and delivering them back to the user. It has support for middlewares and providers.

Usage

import server from "@acai/server";

// we are going to use Açai's router to control our routes, but you can easily overwrite this, will be shown next
const route = server.getRoute();

route.options({ middleware: ["test"] }, () => {
  route.get("/", "file/test@index");
});

const instance = new server();

// Middlewares are optional
instance.addMiddleware("test", (data, next) => next(data));

instance.run();

Overwriting router

If you do not wish to use our router, you can easily overwrite it:

const instance = new server();

instance.setOnRequest((url: string, method: string) => {
  return {
    route: match.path,
    // file to load from the route match
    controller: ".",
    // method to call inside of the file (optional)
    method: "index",
    // extra options, such as middleware, etc
    options: match.options,
    // variables match from the dynamic route
    params: match.variables,
    // query params
    query: query,
    // request body
    fields: content.fields,
    // request files (in case of formdata)
    files: content.files,
  };
});

You can use this for example, if you wish to point all routes to a single frontend, in case of a SPA.

Providers

Providers are the main way to boot things in your application, helping you setup your application in an organized manner.

const instance = new server();

class Provider {
  public boot() {
    /* do something */
  }
}

// aliased middleware
instance.addProvider(Provider);

instance.run();

Middlewares

Middleware is a pipeline that runs before the request actually reaches your code, making changes to the request content, or even bailing it out. You can use them for authentication, validation or anything alike.

const instance = new server();

// aliased middleware
instance.addMiddleware("test", (data, next) => next(data));

// grouped aliased middlewares
instance.addMiddlewares({
  test: (data, next) => next(data),
  auth: (data, next) => "404",
});

// global middleware
instance.addGlobalMiddleware([(data, next) => next(data)]);

instance.run();
  • global middlewares will run in every request of your application
  • aliased middleware are middlewares that you can call through your routes, for privileged requests, and such.

Responses

We provide you with a response utility method that can help you import files, return request status codes, etc. Remember that the server smart guesses from the response you are giving, turning json into actual json and etc.

import { response } from "@acai/server";

// return this to the server to load a file from the project root
response().view("./views/index.html");
// change the response status code
response().status(201).data({});
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文