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

@avil13/koa-router

v0.7.0

Published

Koa-router, with yaml configuration and care of the developer.

Downloads

47

Readme

@avil13/koa-router

Koa-router, with yaml configuration and care of the developer.

Example of project


Install

npm install koa @avil13/koa-router

Code examples:


import Koa from 'koa';
import { makeRouter } from '@avil13/koa-router';
import { join } from 'path';

const PORT = 3000;

const app = new Koa();

const pathToConfig = join(__dirname, 'router-config.yaml');

app.use(makeRouter({
  pathToConfig,
  koaBody: { // Now here out of the box
    multipart: true,
    urlencoded: true,
  }
));

app.listen(PORT);

console.log(`http://localhost:${PORT}/`);

If you want to use validation in a YAML file, I recommend that you set redhat.vscode-yaml

router-config.yaml

# yaml-language-server: $schema=https://raw.githubusercontent.com/avil13/avil13-koa-router/master/packages/koa-router/src/avil13-koa-router-json-schema/schema.json

options:                            # optional
  middlewarePath: ./middleware      # default "middleware"
  controllerPath: ./controller      # default "controller"

middleware:
  is_auth: is-auth.middleware::checkAuth  # path to file "./middleware/is-auth.middleware.ts"
  is_admin: is-admin.middleware           # path to file "./middleware/is-admin.middleware.ts"

routes:
  # simplest example with required params
  - name: BlogList
    path: /blog
    controller: blog::getArticleList # path to file ./controller/blog.ts"

  - name: ShowBlogItem            # alias, available via "ctx.router.name"
    prefix: /blog
    path: /:id                    # => /blog/:id   - since there is a prefix
    controller: blog::getArticle  # In the package from "options.controllers" in the "blog.ts " using the "getArticle" function `export const getArticle =(...) => {...}`
    methods: GET|HEAD             # default all 'GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'
    middleware:
      - is_auth
      - is_admin
    response:
      header: text/html
      status: 200

  # /download/(.*) - is subpath of /(.*)
  # So we put it in early.
  - name: StaticDownload
    path: /download/(.*)
    static: ./public
    download: true

  # Static files
  - name: Static
    path: /(.*)
    static: ./public          # path to static folder

Route example

// ./controller/blog.ts

import { RouteController } from '@avil13/koa-router';

export const getArticleList: RouteController = (ctx) => {
  return [
    'Hello world!!!',
    'Daily news'
  ];
};

export const getArticle: RouteController = (ctx) => {
  const id = ctx.route.params.id;
  return `Article id ${ id }`;
};

Middleware example

Not to be confused with Middleware for the main application.

This is the Middleware for the router.

// ./middleware/is-auth.middleware.ts
import { RouterMiddleware } from '@avil13/koa-router';

export const checkAuth: RouterMiddleware = (ctx) => {
  // check cookies or JWT ...
  if (isNotAuth) {
    return false;
  }

  return true; // is valid
}

CLI

You can use the CLI helpers.

# example
yarn ts-node ./src/index.ts \ # path to your server script
                  route:list  # display the current Routes

route:list

  • You can see the current routers