Git Product home page Git Product logo

fastify-router's Introduction

fastify-router

A simple router loader for fastify servers.

Warning, this is junky. But hey, if it works, don't fix it! (actual suggestions are welcome :p) Please note I use this for my personal projects, its not meant to be fast, efficient or smart and certinaly wasn't designed to be used in production. I created this so I don't have to setup routes with every new webserver I create and I couldn't find anything on fastify that did this. It saves time :D

Read through this entire document for instructions on how to use this!

Installation

Install via NPM (https://www.npmjs.com/package/fastifyrouter.js) npm i fastifyrouter.js

Use

Here's how to use this tool

On your webserver setup file

Call the loadRoutes function, inject fastify and options and voila!

import { loadRoutes } from '/path/to/index.mjs'
//or from npm
import { loadRoutes } from 'fastifyrouter.js';

//Example fastify server 
import Fastify from 'fastify';
const fastify = Fastify();

await loadRoutes(fastify, { dir: './src/routes/', log: true, method: 'GET', prefix: '/home' });

Options:

  • dir: the directory of all the routes you want to load, its using the current module dir so if the module is in node_modules/blah and your routes in src/routes, you will have to set it as ../src/routes
  • log: log to console some debug/verbose messages [optional]
  • method: which method to use as a default (if one not defined in the file name) [optional]
  • prefix: add a prefix to the path. if you are using a folder organization structure, the prefix will not be included by default, so we add this option to mitage this (see issue #3)

On your route file

Export a default (async) function with two properties: request & reply (as fastify does)

export default async function (request, reply) {
    return reply.send({ hello: 'world' });
};

Paths, params, queries, etc (Folder structure)

I basically go by the principal that the folder path is also your path. files named index will be treated as the end/trail of that path. So if you want to render the page /path/to/page you will have a folder structure of /src/routes/path/to/page/index.js or /src/routes/path/to/page.js depends on how you want to organize things :)

For params, because on windows you can't use : for file names instead I do []. So lets say you have param people you will define it in a path like this: /path/to/[people]/index.js -> /path/to/:people/

Queries are handled by fastify already, you don't have to do special definitions.

But what about REST?

All routes MUST contain the rest method in the file name, followed by a - and the path. example: GET-index.js, POST-index.js, SET-index.js etc. If the a method does NOT contain one, it will use the default method you set in the options. (if none is set, it will use GET) I can not verify if ANY/ALL works, as my results are inconclusive. If you can replicate/use, please make an issue request so I can remove this statement :)

Contributing:

  • By code: Make a pr! Add comments to your code and have proper variable names!
  • By suggestions/bugs: Make an issue request! Have as many details as you can so I can replicate and fix!
  • By donating: ko-fi

fastify-router's People

Contributors

auroraisluna avatar

Watchers

 avatar

fastify-router's Issues

if options.dir has trailing slash, error is thrown

Using the example:

import { loadRoutes } from 'fastifyrouter.js';

import Fastify from 'fastify';
const fastify = Fastify();

await loadRoutes(fastify, { dir: './routes', log: true, method: 'GET' });```

Module will work. However, if dir is set to ./routes/ instead you get this error:

AssertionError [ERR_ASSERTION]: The first character of a path should be `/` or `*`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.