0.3.0 • Published 3 years ago

koa-routes-loader v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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