Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lark-router

Package Overview
Dependencies
Maintainers
3
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lark-router

An koa route initialization and configuration module.

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-88.24%
Maintainers
3
Weekly downloads
 
Created
Source

lark-router

Router for lark based on koa 2.0

NPM version build status

Installation

$ npm install lark-router

API

app.use(new Router().load('controllers').routes())

Exmaple:

import Koa    from 'koa';
import Router from 'lark-router';

const router = new Router().load('controllers');

const app = new Kao();

app.use(router.routes());

app.listen(3000);

load

routes

lark-router extends koa-router with a method load(directory, prefix). By calling router.load(directory, prefix), lark-router will load all js files recursively under that directory, and use their exports as callbacks to the routes corresponding to their paths.

This is how file paths is converted into routes (with default options: { default: 'index.js', param_prefix: '_'})

directory
  ├─ index.js         => /
  ├─ hello/
  │     └─ world.js   => /hello/world
  └─ _category/
        └─ _title.js  => /:category/:title
methods

Methods should be defined in those js files, exported as verb properties. We recommand you use verbs in upper case to avoid using reserved words such as delete.

/**
 * @file: hello/world.js
 **/
 
export const GET = async (ctx, next) => {
    // handle requests on GET /hello/world
}

export const DELETE = async (ctx, next) => {
    // handle request on DELETE /hello/world
}

or use router directly by exporting a function

/**
 * @file: hello/world.js
 **/

export default router => {
    router.get('/', async (ctx, next) => {
        // handle requests on GET /hello/world
    }
    router.get('/:foo/:bar', async (ctx, next) => {
        // handle requests on GET /hello/world/:foo/:bar
    }
}

Tests

npm test

Keywords

FAQs

Package last updated on 05 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc