Giter Site home page Giter Site logo

maranda-koa2-router's Introduction

useage

npm i --save maranda-koa2-router

for the complete maranda koa server case, with static, session-mysql, koabody, router

this example is used for ts, if you are js user, if can use as almost the same, just delete the type define.

//app.ts
import Router,{ koaRouter } from 'maranda-koa2-router';
import Koa from 'koa';
import { join as pathJoin } from 'path'

interface Ctx {
    // your custom context
}
export type Route = Router.Route<any, Ctx>;
export type Middleware = Router.Middleware<any, Ctx>;

const app = new Koa<any,Ctx>();
const router = new Router<any, Ctx>(pathJoin(__dirname,'Routers'),{..});
router.get('/', async(ctx, next)=>{
    ...
    await next();
    ...
});
app.use(router.routes())

app.listen(8080)
console.log('app started at port 8080...');

//Routers/ajax/text.ts
import { Route } from '../../app';
import { middleware1 } from '../Middlewares/a'
import { middleware2 } from '../Middlewares/b'
import { middleware3 } from '../Middlewares/c'

const a0:Route = {
    path: '/a0',
    methods: ['get'],
    middleware: middleware1
}
const b1:Route ={
    path: ['/b1','/b1/x'],
    methods: ['get'],
    middleware: [middleware2,middleware3]
}
export {a0, b1}

//Middlewares/a.ts
import { Middleware } from '../../app'
export middleware1:Middleware = aysnc (ctx, next) => {
    ...
    await next();
    ...
}
// type2
//app.ts
const router = Router.LoadRoutes<any, Ctx>(pathJoin(__dirname,'Routers'),{..});
....
router.then((rt)=>{
    rt.get('/', async(ctx, next)=>{
        ...
        await next();
        ...
    });
    app.use(rt.routes())
    app.listen(8080)
    console.log('app started at port 8080...');
})

for more Versions, click see the changelog

maranda-koa2-router's People

Stargazers

Thales Hume avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.