5.0.0 • Published 7 years ago
koa-minimal-router v5.0.0
koa-minimal-router
Minimal router for koa.js.
- Simplest approach possible
- Express-like path parameters
- Custom HTTP methods if needed
- Use with
koa-compose
for seamless modularization - Typescript definitions included
Usage
Installation
npm install --save koa-minimal-router
# or
yarn add koa-minimal-router
Example 1 (Basic)
const { GET } = require("koa-minimal-router");
const Koa = require("koa");
const app = new Koa();
app.use(GET("/foo")(async ctx => {
ctx.body = "Hello World!";
}));
app.listen(3000);
Example 2 (Path parameters)
const { GET } = require("koa-minimal-router");
const Koa = require("koa");
const app = new Koa();
app.use(GET("/foo/:fooId")(async ctx => {
console.log(ctx.params.fooId); // <- access url parameters from ctx.params
}));
app.listen(3000);
Example 3 (Custom http methods)
const router = require("koa-minimal-router"); // <- importing the router
const Koa = require("koa");
const app = new Koa();
app.use(router("GET")("/foo")(async ctx => { // <- specifying your http method
ctx.body = "Hello World!";
}));
app.listen(3000);
Example 4 (Modularization)
// foo.js
const { GET , PUT } = require("koa-minimal-router");
const compose = require("koa-compose");
module.exports = compose([ // <- koa-compose for middleware composition
GET("/foo")(async ctx => { ... }),
PUT("/foo")(async ctx => { ... }),
]);
// bar.js
const { GET , PUT } = require("koa-minimal-router");
const compose = require("koa-compose");
module.exports = compose([
GET("/bar")(async ctx => { ... }),
PUT("/bar")(async ctx => { ... }),
]);
// index.js
const Koa = require("koa");
const fooRouter = require("./foo");
const barRouter = require("./bar");
const app = new Koa();
app.use(fooRouter);
app.use(barRouter);
app.listen(3000);
Example 5 (Modularization without koa-compose
)
// foo.js
const { GET , PUT } = require("koa-minimal-router");
module.exports = app => {
app.use(GET("/foo")(async ctx => { ... }));
app.use(PUT("/foo")(async ctx => { ... }));
);
// index.js
const Koa = require("koa");
const fooRouter = require("./foo");
const app = new Koa();
fooRouter(app);
app.listen(3000);
Example 6 (Typescript)
// foo.ts
import { GET } from "koa-minimal-router";
import * as compose from "koa-compose";
export default compose([
GET("/foo")(async ctx => { ... }),
GET("/foo/:fooId")<"fooId">(async ctx => { ... }), // <- ctx.params is typesafe now
GET("/foo/:fooId/:barId")<"fooId"|"barId">(async ctx => { ... }),
]);
// index.ts
import * as Koa from "koa";
import fooRouter from "./foo";
const app = new Koa();
app.use(fooRouter);
app.listen(3000);
Example 7 (Middleware injection)
const { GET } = require("koa-minimal-router");
const Koa = require("koa");
const compose = require("koa-compose");
const app = new Koa();
const addFooHeader = async (ctx, next) => {
ctx.set('X-Foo', 'foo');
await next();
}
app.use(GET("/foo")(compose([
addFooHeader,
async ctx => {
ctx.body = "Hello World!";
},
])));
app.listen(3000);
Example 8 (Middleware mount)
const { mount } = require("koa-minimal-router");
const Koa = require("koa");
const compose = require("koa-compose");
const app = new Koa();
app.use(mount("/foo")(async ctx => {
ctx.body = "Hello World!";
}));
app.listen(3000);
Example 9 (App mount)
const { mount } = require("koa-minimal-router");
const Koa = require("koa");
const compose = require("koa-compose");
const app = new Koa();
const otherApp = new Koa();
app.use(async ctx => {
ctx.body = "Hello World!";
})
app.use(mount("/foo")(otherApp));
app.use(GET("/foo")(otherApp));
app.listen(3000);