A Fastify plugin that provides a file system routes, based on the way Next.JS file system routing works, including all possible features.
1. File System Routing.
2. Index Routes.
3. Nested Routes.
4. Dynamic Route Segments.
5. Catch All (Wildcard \*) Routes.
6. Multiple parameters. eg /users/:id-:name
npm install fastify-file-routes
yarn add fastify-file-routes
import { fileRoutes } from "fastify-file-routes";
const app: FastifyInstance = fastify({ logger: true });
await app.register(fileRoutes, {
routesDir: "./routes",
prefix, // -> optional
});
await app.listen(3000);
mkdir routes
//file: `routes/some/route.ts`
//url: `http://localhost/some/route`
import type { Route } from "fastify-file-routes";
export const routes: Route = {
get: {
handler: async (request, reply) => {
await reply.send({
some: "route",
});
},
},
};
//file: `routes/users/[userId]/settings.js`
//mapped to: `http://localhost/users/:userId/settings`
export const routes: Route = {
get: {
handler: async (request, reply) => {
const { params } = request;
await reply.send(`photos of user ${params.userId}`);
},
},
};
//file: `routes/profile/[...id].ts `
//mapped to: `http://localhost/profile/*`
export const routes: Route = {
get: {
handler: async (request, reply) => {
const { params } = request;
await reply.send(`wildcard route`);
},
},
};
export const routes: Route = {
post: {
handler: async (_request, reply) => {
await reply.send({
post: "post user",
});
},
},
};
//file: `routes/some/route.ts`
//url: `http://localhost/api/some/route`
await app.register(fileRoutes, {
routesDir: "./routes",
prefix: "/api",
});
export const routes: Route = {
post: {
handler: async (_request, reply) => {
await reply.send({
post: "post user",
});
},
},
};
//file: `routes/some/[param1]-[param2].ts`
//url: `http://localhost/some/:param1-:param2`
await app.register(fileRoutes, {
routesDir: "./routes",
});
export const routes: Route = {
post: {
handler: async (_request, reply) => {
await reply.send({
post: "multiple params",
});
},
},
};
- Check the examples folder in /examples to see how to use the plugin.
- route.prefixTrailingSlash has been set to 'both'.
Method specification for attributes is available here: Method specification
βΉοΈ attributes
url
andmethod
are dynamically provided
Allowed attributes mapped to Http methods in module:
- delete
- get
- head
- patch
- post
- put
- options
to skip file in routes directory, prepend the .
or _
character to filename
examples:
routes
βββ .ignored-directory
βββ _ignored-directory
βββ .ignored-js-file.js
βββ _ignored-js-file.js
βββ .ignored-ts-file.ts
βββ _ignored-ts-file.ts
βββ ignored-js-test.test.js
βββ ignored-ts-test.test.ts
β οΈ also any*.test.js
and*.test.ts
are skipped!
this is useful if you want to have a lib file which contains functions that don't have to be a route, so just create the file with _
prepending character
- Adding support for optional wildcard routes - [[...id]].
- More tests.
- Better typescript stuff for validation and inferences in routes.
Fastify - AutoRoutes - Lots of code has been used from this project.