最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Fastify Route Auto Grouping Issue (CmdProtocol & CloudTask) - Stack Overflow

programmeradmin3浏览0评论

When registering routes in Fastify using register(), routes with the same first letter are automatically grouped under a common node.

Code for Route Registration (routes/index.js):

import { LoginRouter } from "./Login.js";
import { NetProtocolRouter } from "./NetProtocol.js";
import { CmdProtocolRouter } from "./CmdProtocol.js";
import { CloudTaskRouter } from "./CloudTask.js";
import { BotTelegramRouter } from "./BotTelegram.js";
import { EventTrrigerRouter } from "./EventTrriger.js";

export default async function router(fastify, _options) {
    fastify.register(async (instance) => {
        await instance.register(LoginRouter, { prefix: "/login" });
        await instance.register(NetProtocolRouter, { prefix: "/netProtocol" });
        await instance.register(CmdProtocolRouter, { prefix: "/cmdProtocol" });
        await instance.register(EventTrrigerRouter, { prefix: "/eventTrriger" });
        await instance.register(BotTelegramRouter, { prefix: "/botTelegram" });
        await instance.register(CloudTaskRouter, { prefix: "/cloudTask" });
    }, { prefix: '/api' });
}

printRoutes() Output:

└── (empty root node)
    ├── /
    │   └── api/
    │       ├── login (POST)
    │       │   └── / (POST)
    │       ├── netProtocol (POST, GET, HEAD)
    │       │   └── / (POST, GET, HEAD)
    │       ├── c
    │       │   ├── mdProtocol (POST)
    │       │   │   └── / (POST)
    │       │   └── loudTask/TallyingLotto/ (POST)
    │       ├── eventTrriger/UpdateLotto/ (POST)
    │       └── botTelegram/telegram/webhook/ (POST)
    └── * (OPTIONS)

cmdProtocol and cloudTask are grouped under a single c node instead of being separate routes. Expected Output:

├── /api/
│   ├── login (POST)
│   ├── netProtocol (POST, GET, HEAD)
│   ├── cmdProtocol (POST)
│   ├── cloudTask/TallyingLotto (POST)
│   ├── eventTrriger/UpdateLotto (POST)
│   ├── botTelegram/telegram/webhook (POST)

Solutions Tried:

  1. Enabling caseSensitive: true

    Did not work

    const fastify = Fastify({
        caseSensitive: true, // Prevents auto-grouping by case sensitivity
    });
    

    Still grouped under c

  2. Registering routes individually Did not work

    await fastify.register(CmdProtocolRouter, { prefix: "/api/cmdProtocol" });
    await fastify.register(CloudTaskRouter, { prefix: "/api/cloudTask" });
    

    Still grouped under c

  3. Using relative paths inside route files Did not work

    export default async function CmdProtocolRouter(fastify, _options) {
        fastify.post('/', async (request, reply) => {
            reply.send({ success: true });
        });
    }
    

    Still grouped under c

Why is Fastify automatically grouping routes with the same first letter? Is there an official way to prevent this automatic grouping behavior? Is this a bug, or is there an intended optimization in Fastify that causes this?

When registering routes in Fastify using register(), routes with the same first letter are automatically grouped under a common node.

Code for Route Registration (routes/index.js):

import { LoginRouter } from "./Login.js";
import { NetProtocolRouter } from "./NetProtocol.js";
import { CmdProtocolRouter } from "./CmdProtocol.js";
import { CloudTaskRouter } from "./CloudTask.js";
import { BotTelegramRouter } from "./BotTelegram.js";
import { EventTrrigerRouter } from "./EventTrriger.js";

export default async function router(fastify, _options) {
    fastify.register(async (instance) => {
        await instance.register(LoginRouter, { prefix: "/login" });
        await instance.register(NetProtocolRouter, { prefix: "/netProtocol" });
        await instance.register(CmdProtocolRouter, { prefix: "/cmdProtocol" });
        await instance.register(EventTrrigerRouter, { prefix: "/eventTrriger" });
        await instance.register(BotTelegramRouter, { prefix: "/botTelegram" });
        await instance.register(CloudTaskRouter, { prefix: "/cloudTask" });
    }, { prefix: '/api' });
}

printRoutes() Output:

└── (empty root node)
    ├── /
    │   └── api/
    │       ├── login (POST)
    │       │   └── / (POST)
    │       ├── netProtocol (POST, GET, HEAD)
    │       │   └── / (POST, GET, HEAD)
    │       ├── c
    │       │   ├── mdProtocol (POST)
    │       │   │   └── / (POST)
    │       │   └── loudTask/TallyingLotto/ (POST)
    │       ├── eventTrriger/UpdateLotto/ (POST)
    │       └── botTelegram/telegram/webhook/ (POST)
    └── * (OPTIONS)

cmdProtocol and cloudTask are grouped under a single c node instead of being separate routes. Expected Output:

├── /api/
│   ├── login (POST)
│   ├── netProtocol (POST, GET, HEAD)
│   ├── cmdProtocol (POST)
│   ├── cloudTask/TallyingLotto (POST)
│   ├── eventTrriger/UpdateLotto (POST)
│   ├── botTelegram/telegram/webhook (POST)

Solutions Tried:

  1. Enabling caseSensitive: true

    Did not work

    const fastify = Fastify({
        caseSensitive: true, // Prevents auto-grouping by case sensitivity
    });
    

    Still grouped under c

  2. Registering routes individually Did not work

    await fastify.register(CmdProtocolRouter, { prefix: "/api/cmdProtocol" });
    await fastify.register(CloudTaskRouter, { prefix: "/api/cloudTask" });
    

    Still grouped under c

  3. Using relative paths inside route files Did not work

    export default async function CmdProtocolRouter(fastify, _options) {
        fastify.post('/', async (request, reply) => {
            reply.send({ success: true });
        });
    }
    

    Still grouped under c

Why is Fastify automatically grouping routes with the same first letter? Is there an official way to prevent this automatic grouping behavior? Is this a bug, or is there an intended optimization in Fastify that causes this?

Share Improve this question edited Feb 15 at 15:24 jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked Feb 15 at 15:18 Dongpyo LeeDongpyo Lee 192 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The documentation explain it a bit https://fastify.io/docs/latest/Reference/Server/#printroutes

The merged tree doesn't represent the internal router structure. Do not use it for debugging.

What you can see, it is the radix tree that is built to route an http request to the right handler. So, you should ignore any grouping - it is not a bug nor an issue. It is the secret source of the fastify speed.

If you want to see the structure of your application I would suggest to check this plugin instead: https://github/Eomm/fastify-overview

发布评论

评论列表(0)

  1. 暂无评论