@antalervin19/fastrouter
v1.0.1
Published
A lightweight file-based API router for Fastify
Maintainers
Readme
FastRouter
A small file-based API router for Fastify.
FastRouter lets you define your API routes in one place while automatically loading the handler files from your routes directory.
Features
- Built for Fastify
- Automatic route handler loading
- Simple route definitions
- Supports GET, POST, PUT, PATCH, DELETE, HEAD and OPTIONS
- TypeScript support
- No unnecessary framework magic
Quick Start
Install FastRouter:
npm install @antalervin19/fastrouterCreate a routes directory:
routes/
├── hello.ts
└── users/
└── me.tsCreate a Fastify server:
import Fastify from "fastify";
import { FastRouter } from "fastrouter";
const server = Fastify();
const router = new FastRouter(server, {
directory: "./routes"
});
router.get("/hello");
router.get("/users/me");
await router.load();
await server.listen({
port: 3000
});FastRouter automatically maps:
/helloto:
routes/hello.tsand:
/users/meto:
routes/users/me.tsA route handler looks like this:
import type { FastifyRequest, FastifyReply } from "fastify";
export default async function (
request: FastifyRequest,
reply: FastifyReply
) {
return reply.send({
message: "Hello from FastRouter!"
});
}Example
A complete working example is included in the repository.
Run it with:
npm run exampleThen open:
http://127.0.0.1:3000/helloDocumentation
See DOCUMENTATION.md for the full API documentation.
License
MIT
