koa-router-with-directory
TypeScript icon, indicating that this package has built-in type declarations

1.12.0 • Public • Published

koa-router-with-directory

attachDirToRouter() attaches your directory into koa-router.
The installations of koa and koa-router on your own are pre-requisites of this package.
See koa-router-with-directory.netlify.app also.

npm NPM npms.io (quality) Libraries.io dependency status for latest release Maintenance

Installation

npm install koa-router-with-directory

Usage

call attachDirToRouter before start koa server

import Koa from "koa";
import Router from "koa-router";
import { attachDirToRouter } from "koa-router-with-directory";
import * as path from "path";

const router = new Router();

const app = new Koa();
const port = 8080;

attachDirToRouter(router, path.join(__dirname, "routes/"))
  .then((rrouter) => {
    app.use(router.routes());
    app.use(router.allowedMethods());
    app.listen(port, () => {
      console.log(`listening ${port}`);
    });
  });

the project directory should be like below.

project
|- routes
| |- some
| | |_ path
| |   |_ index.js
| |
| |_ another
|   |_ path
|     |_ index.js
|
|_ server.js

routed files should be named "index.js" and have one or more methods named get|post|put|delete.

import { Context } ffrom "koa";
export function get(ctx: Context){}
export function post(ctx: Context){}
export function put(ctx: Context){}
export function Delete(ctx: Context){}

/koa-router-with-directory/

    Package Sidebar

    Install

    npm i koa-router-with-directory

    Weekly Downloads

    9

    Version

    1.12.0

    License

    MIT

    Unpacked Size

    48.2 kB

    Total Files

    12

    Last publish

    Collaborators

    • tomsd