Skip to content

vite-plugin-fastify-routes

File-based routing for Fastify applications using Vite.

Installation

Install vite-plugin-fastify-routes with your favorite package manager:

sh
npm i vite-plugin-fastify-routes -D
sh
yarn add vite-plugin-fastify-routes -D
sh
pnpm i vite-plugin-fastify-routes -D
sh
bun add vite-plugin-fastify-routes -D

Usage

Add Scripts

Add the following scripts to your package.json file:

json
{
  // ...
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  }
  // ...
}

Configure Vite

Configure Vite by creating a vite.config.ts file in the root directory of your project, as shown below:

ts
// vite.config.ts
import { resolve } from 'path';
import { defineConfig } from 'vite';
import fastify from 'vite-plugin-fastify';
import fastifyRoutes from 'vite-plugin-fastify-routes'; 

export default defineConfig({
  server: {
    host: '127.0.0.1',
    port: 3000,
  },
  plugins: [
    fastify(),
    fastifyRoutes(), // Default: { routesDir: './src/routes' }
  ],
  resolve: {
    alias: {
      '~': resolve(__dirname, 'src'),
    },
  },
});

Create the Fastify Application

Create a Fastify application by defining src/app.ts:

ts
// src/app.ts
import type { FastifyServerOptions } from 'fastify';
import fastify from 'fastify';

import router from '~/plugins/router';

const app = async (options: FastifyServerOptions = {}) => {
  const app = fastify(options);

  app.register(router);

  return app;
};

export default app;

Start the Server

Start the server by defining src/server.ts:

ts
// src/server.ts
import app from './app';

const start = async () => {
  const server = await app({
    logger: {
      transport: {
        target: '@fastify/one-line-logger',
      },
    },
  });

  try {
    server.listen({ host: '127.0.0.1', port: 3000 });
  } catch (err) {
    server.log.error(err);
    process.exit(1);
  }
};

start();

Create the Router Plugin

Create the router plugin by defining src/plugins/router.ts:

ts
// src/plugins/router.ts
import plugin from 'fastify-plugin';

import routes from 'virtual:fastify-routes'; 

export default plugin(
  async (app) => {
    routes(app, { prefix: '/api' }); 
  },
  { name: 'router' },
);

Type

ts
// vite-env.d.ts
/// <reference types="vite-plugin-fastify-routes/client" />

Released under the MIT License.