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

koa-routes-loader

v0.3.0

Published

Load routes automatically from file system for Koa applications.

Downloads

5

Readme

koa-routes-loader


Build Status Coverage Status NPM version License Code Size

Load routes automatically from file system for Koa applications ⚡.

Features

  • 🚀   Amiable and elegant routes loader.
  • 💅🏻   Glamorous and clean router style.
  • 🔥   Use koa-router under the hood.
  • 🪀   Support for prefix for all routes.
  • 🎳   Support for shared middlewares between all routes.
  • 🪁   Support for single and multi-methods on each route.
  • 🎯   Support for specific middlewares on each route.
  • ⚖️ Tiny bundle size.

Installation

# npm
$ npm install koa-routes-loader
# yarn
$ yarn add koa-routes-loader

Usage

This is a practical example of how to use.

// routes/users.routes.js
module.exports = {
  prefix: '/users',
  middelwares: [
    (ctx, next) => {
      ctx.state = { count: 0 };
      next();
    }
  ],
  routes: [
    {
      path: '/',
      method: 'GET',
      middelwares: [
        (ctx, next) => {
          ctx.state = { count: ctx.state.count + 20 };
          next();
        }
      ],
      handler: (ctx) => {
        ctx.body = { conter: ctx.state.count };
      }
    }
  ]
};
const path = require('path');
const Koa = require('koa');
const koaRouterLoader = require('koa-routes-loader');

const app = new Koa();
koaRouterLoader(app, { dir: path.join(__dirname, 'routes') });

app.listen(3000);
// /users GET 200 { coutner: 20 }

GUIDE

1. Create your routes folder (folder_name).

$ cd myapp      # change your directory to your application.
$ mkdir routes  # create the routes folder.

2. Create your routes file (file_name.js).

$ cd routes && touch user.js

3. Create your routes schema.

Here, should you understand each key can you use in the schema.

{
  prefix: '/',            // prefix for all routes presented in this file. (string)
  middelwares: [],        // middlewares shared between all routes presented in this file. (array)
  routes: [               // routes should loaded. (array)
    {
      path: '/',          // path of the current route. (string)
                          // Note:
                          //  - should present method or methods not both.
                          //  - if exist both, we use methods.
      method: '',         // method of the current route (one only). (string)
      methods: [],        // methods of the current route (more than one). (array)
      middelwares: [],    // middlewares used only with the current route. (array)
      handler: () => {}   // handler used by the router. (function)
    }
  ]
}

4. Load all the routes from files.

// const koaRouterLoader = require('koa-routes-loader');

koaRouterLoader(
  koaApplication, // koa instance. (Koa)
  {
    dir: '', // path for your `routes_folder_name`. (string)
    glob: {}, // glob options passed directly to Glob module. (object)
    useFilePrefix: false, // boolean tell the loader to use the file
    // name inside the route path -/prefix/fileName/routePath-. (boolean)
    autoPlural: true, // boolean tell the loader to add `s` as suffix
    // to the used fileName -work only when useFilePrefix set to true-. (boolean)
    allowedMethods: null // your custom allowed methods. (object)
  }
);

License


MIT © Imed Jaberi