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

fastifyrouter.js

v1.0.3

Published

A simple router loader for fastify servers

Downloads

2

Readme

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