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:
Enabling
caseSensitive: true
Did not work
const fastify = Fastify({ caseSensitive: true, // Prevents auto-grouping by case sensitivity });
Still grouped under c
Registering routes individually Did not work
await fastify.register(CmdProtocolRouter, { prefix: "/api/cmdProtocol" }); await fastify.register(CloudTaskRouter, { prefix: "/api/cloudTask" });
Still grouped under c
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:
Enabling
caseSensitive: true
Did not work
const fastify = Fastify({ caseSensitive: true, // Prevents auto-grouping by case sensitivity });
Still grouped under c
Registering routes individually Did not work
await fastify.register(CmdProtocolRouter, { prefix: "/api/cmdProtocol" }); await fastify.register(CloudTaskRouter, { prefix: "/api/cloudTask" });
Still grouped under c
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 badges1 Answer
Reset to default 0The 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