Simple routing for Koa
npm install mountain-js
const route = require("mountain-js");
let app = koa();
app.use(route("GET", "/posts", function *(next) {
this.body = "got posts!";
}));
app.listen();
Allow route on all methods.
app.use(route("/posts", function *(next) {
this.body = "always got posts!";
}));
Param'd URLs.
app.use(route("GET", "/posts/:id", function *(next) {
this.body = "got post #" + this.params.id;
}));
More middlewares.
let myMiddleware = function *(next) {
yield next;
}
app.use(route("POST", "/posts", myMiddleware, koaBody(), function *(next) {
this.body = "posted to posts";
}));
Jump back out to the app from the routed middlewares
app.use(route("GET", "/posts", function *(next) {
this.body = "posted to posts";
yield next;
}));
app.use(function *(next) {
// hello!
});
MIT