npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-file-routes

v1.1.2

Published

A Fastify plugin that provides a file system routes, based on the way Next.JS file system routing works, including all possible features.

Downloads

228

Readme

Fastify File Routes

Banner

NPM downloads npm node-current

A Fastify plugin that provides a file system routes, based on the way Next.JS file system routing works, including all possible features.

:sparkles: 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

:rocket: Installation

npm install fastify-file-routes
yarn add fastify-file-routes

:blue_book: Usage/Examples

1. Register the Plugin.

import { fileRoutes } from "fastify-file-routes";

const app: FastifyInstance = fastify({ logger: true });

await app.register(fileRoutes, {
  routesDir: "./routes",
  prefix, // -> optional
});

await app.listen(3000);

2. Create the routes directory.

mkdir routes

3. Create your first route in the routes directory

//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",
      });
    },
  },
};

4. Access the Parameters.

//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}`);
    },
  },
};

5. Wildcard (*) routes.

//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`);
    },
  },
};

6. Post Request..

export const routes: Route = {
  post: {
    handler: async (_request, reply) => {
      await reply.send({
        post: "post user",
      });
    },
  },
};

7. Prefix Route

//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",
      });
    },
  },
};

8. Multiple Parameters

//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",
      });
    },
  },
};

:information_source: Info

  1. Check the examples folder in /examples to see how to use the plugin.
  2. route.prefixTrailingSlash has been set to 'both'.

:arrow_forward: Route module definition

Method specification for attributes is available here: Method specification

:information_source: attributes url and method are dynamically provided

Allowed attributes mapped to Http methods in module:

  • delete
  • get
  • head
  • patch
  • post
  • put
  • options

:arrow_forward: Skipping files

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

:warning: 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

TODO

  1. Adding support for optional wildcard routes - [[...id]].
  2. More tests.
  3. Better typescript stuff for validation and inferences in routes.

Visualization of this Repo.

Visualization of this repo

License

MIT

Related/Acknowledgements

Fastify - AutoRoutes - Lots of code has been used from this project.