Skip to content
当前有符合你浏览器所设置语言的版本,是否前往zh-CN版本的网站?
There is a version suitable for your browser's language settings. Would you like to go to the zh-CN language of the site?
HomeDocument

Router

H1

In the Introduction section of Milkio, you must have experienced the “magic” of router. In Milkio, you hardly need to write router code yourself. Milkio automatically reads files in the /src/apps directory and generates routes for all your APIs.

Customizing Routing Logic

In most cases, you will only need the routes generated by Milkio. However, there may be some special cases where you need to customize your routes. In such cases, you can edit the /src/router.ts file. Its content will be similar to the following:

export const routerHandler = async (path: string, fullurl: URL) => {
// ...
return false;
};

You can edit the content of this method to supplement the routing logic beyond what Milkio generates automatically. You can return an existing API address, and it will be called. If you return false, a failure message of NOT_FOUND will be returned.

This method accepts two parameters: path and fullurl. path represents the suffix of the request path (removing the domain and ignored path levels). For performance reasons, if the URL contains parameters, these parameters will not be removed and will still be preserved in path, for example, foo/bar?hello=world. fullurl is the complete URL object, and the ignored path levels will not be removed.